views:

142

answers:

2

Hello everyone! I am trying to put a listbox control on my ASP.net page, and when I click it, the selectedindex value is always -1. Why is it doing that? I set AutoPostBack to true. Why is it always returning -1?

Please let me know.

Thanks

+3  A: 

There could be many reasons why but I am guessing that you are loading the contents of the ListBox on every load of the page.

Wrap your data binding code in an if statement like this to allow the control to retain what index you selected:

if (!this.IsPostBack) 
{
    // data binding code here
}
Andrew Hare
Agreed! That would be the most probable cause of the error.
Cerebrus
+1  A: 

It does depend on what your doing, but -1 normally means nothing is selected when a PostBack is occurring, or that the list of items is empty that the control is being databound to.

Mcbeev