Click to See Complete Forum and Search --> : Multi-Select ListBox Problem


MCRoberts
September 15th, 2008, 02:30 PM
Greetings:

I have a simple ListBox with multiselect enabled in an ASP.NET 2.0 WEB project. The box has about 10 items in it and to test, I selected items 1 and 5 (index base 0). Both items in the box are highlighted as you would expect. Also, I checked to ensure that view state was enabled for the list box.

Then, I execute this code straight out of MS-help to determine which items where selected:

int nNumSel = 0;
foreach(ListItem li in ListBox1.Items)
{
if(li.Selected == true)
{
nNumSel++;
}
}

Stepping through this, I am finding that only the 1st selected item is recognized as being selected. The item at index 5 is reporting "false" for its "Selected" property. Why? What have I missed?

Thanks to anybody that responds.
Mark

jspintu
September 17th, 2008, 01:02 AM
Please check whether there is a post back in the page load event. May be this would be the cause.

MCRoberts
September 17th, 2008, 09:05 AM
Hi, thanks for responding.

Not sure what you mean, exactly. Are you talking about checking for whether or not the page is being loaded for the "first time" or as a result of a postback using the "IsPostback" property? Or something else?

The one thing that I wanted to stress was that I am selecting TWO items and it is always detecting the first one, it just isn't seeing the other selections. That is, the "Selected" property is true only for the first selected item.

Cheers,
Mark

jspintu
September 18th, 2008, 02:18 AM
Hi,

Please check that while your selection is getting checked is the data in the listbox is getting refreshed. I mean as you are checking for the selected items in the list, is the list doing a postback. And in that case you can check whether in the page load event have you bound the data to the listbox without handling the postback event. I hope this would solve the problem.

Regards,

Pintu