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
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
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
}
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.