I have a list box control:
The code behind resembles:
<
Databinding DropDownLists/ListBoxes is painful, because they often bind to the wrong values.
I've given up on using DataBind(), and just resort to using a Foreach loop:
foreach (Item i in DataSet)
{
listBox.Items.Add(etc);
}
What's the output of the foo() function call?
Populating manually the list box you can set indexes to whatever you want (all 0 for example) - so the same thing can happen setting a given dataSource under certain circumstances (one that specifies indexes I suppose). If all the item indexes are 0 the result is that the SelectedIndexChanged event is not raised (index does not change!) and everything is messed up: on post-back selection will go back to the first item in the list.
This would explain it - I cannot think of anything else - it is working fine for me on .NET 2.0 I am using an ArrayList with strings to populate the listBox.
The only way I can reproduce your issue is setting all indexes to 0.
I'd say add a watch to the ListBox and check the indexes at runtime to make sure they're not all zeroes.
Load the data in the Page_Init instead of the Page_Load. The data has to be filled during the Page_init to be available in the PostBack.