Hi, This is my 1st post on daniWeb. First i want to mention that i am complete Ametuer(noob) in programming. just trying to work around with C# and need some help.

Ok my problem is that i have a LISTBOX on my page with Selection mode = MULTI, A label which shows the selected items of listbox, a button that triggers the page.

Adding content in list box like this:


Using Event Click on Button like this:

foreach (ListItem my in ListBox1.Items)

            if (my.Selected)

                Label1.Text += " " + ListBox1.SelectedValue;

NOW i'll tell u my problem

1st- When ever i select multi item from listbox. it shows first selected item of the list box 2 times.

sorry if i being stupid..thanks in advance for helping me out

Question: Do you have a method in place to clear the textBox between button clicks?
Question 2: Are you trying multi-selections after trying single selections?

I generally would include a Label1.Text = ""; line prior to populating the Label1.Text (prior to the ForEach statement) to ensure that there are no previously entered values already present in the label prior to being filled with 'new' data.

Unless you have listbox items with identical values the only thing I can think of is that you're adding your list of selected items to an already present single selection.

Hope this helps :) Please remember to mark the thread solved once your issue is resolved.

I put label1.text=""; prior to foreach but it gives me error cuz it clears the selected list before foreach...so i put label1.text=""; on page load...and it solved an untold issue of previously entered value already present...so thnx for that..:)

now BACK to the big issue...
ur Question 1 = Now i m using a method
ur Question 2 = NO.

i m posting 2 images to clear my problem.

Ok, loop problem :twisted:

Personally I like to do my loops like so:

Label1.Text = "";
for (int a = 0; a < ListBox1.Items.Count; a++)
    if (ListBox1.Items[a].Selected == true)
        Label1.Text += " " + ListBox1.Items[a].Value;

I think that might solve your issue. The way your code is written it's taking the "selectedvalue" which I think is basically equating to the most recent or top selected value only and not "all selected values" when writing output.

THANKS MAN the ISSUE is solve....:)

i never thought that daniweb could help me this fast....

THANKS again.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.