I have a list box control:
<asp:ListBox runat="server" id="lbox" autoPostBack="true" />
The code behind resembles:
private void Page_Load(object sender, System.EventArgs e)
{
lbox.SelectedIndexChanged+=new EventHandler(lbox_SelectedIndexChanged);
if(!Page.IsPostBack)
{
LoadData();
}
}
private LoadData()
{
lbox.DataSource = foo();
lbox.DataBind();
}
protected void lboxScorecard_SelectedIndexChanged(object sender, EventArgs e)
{
int index = (sender as ListBox).selectedIndex;
}
My problem is that when my page receives a post back (when a user makes a selection in the listbox), the selection always "jumps" to the first item in the listbox, so that the index variable in my callback function is always 0.
Seems like this may be a viewstate problem? How can I fix it so that the selection index remains through the postback?
There is no ajax going on, this is .NET 1.0.
Thanks.
EDIT 1 JohnIdol has gotten me a step closer, If I switch the datasource from my original DataTable to an ArrayList, then everything work properly...what would cause this?
Edit 2 It turns out that my DataTable had multiple values that were the same, so that the indexes were treated as the same as all items with the same value...thanks to those who helped!
The real issue here is order of events. When you databind in page_load you overwrite the posted data, thats why the selection is not set in the listbox. You can easily overcome this by moving the binding logic to Page_Init.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With