ASP.net ListBox woes ...
Today I was working on a WebUserControl that basically contained two ListBoxes. One contained a list of items available and the other had all the selected items. A problem came up where when I selected an item in the available items list and tried to copy it to the other list, the SelectedIndex reported by the ListBox would be wrong. I tried every trick I could to fix it but the mysterious problem persisted. What puzzled me even more was that I had made a similar control a few days back and it worked flawlessly !
After a couple of hours of trial and error, I stumbled upon the real cause of the problem :
Every Item in the list has two properties, called Text and Value. Text dictates what is to be shown in the ListBox and Value basically acts like an index value. What I observed was that in my case, the Text values were unique but the Values were repeating for a number of items. Given the current circumstances, it was allright to have similar values for multiple items. The problem lies in the ASP.net ListBox which does not like this kind of condition. I had to wrangle with multiple joins to use a different column to bind to the ListItem Value .. but this eventually solved my problem.
Moral of the story :
If your ListBox is acting weird, check your value bound column for repeating values.