I have a ListBox inside DataList control. The Autopostback is set to true in the ListBox.
For some reason when I select an item from the ListBox control and I check in the ListBox_SelectionChanged event it is always the First item of the ListBox items. Why is that?
I just checked and the SelectedIndex is set to 0 even if I select the 5th or 6th item in the ListBox.
>
<ItemTemplate>
<div style="background-color:#FFCC99; border:2px solid black; padding:2px">
<div style="background-color:lightgreen; border:2px solid black; padding:2px">
<%# Eval("NounTypeName") %>
</div>
<asp:ListBox ID="lbNounTypes" AutoPostBack="true" DataTextField="Key" DataValueField="NounTypeId" SelectionMode="Single"
DataSource='<%# Eval("Attributes") %>' runat="server"
onselectedindexchanged="lbNounTypes_SelectedIndexChanged">
</asp:ListBox>
protected void lbNounTypes_SelectedIndexChanged(object sender, EventArgs e)
{
var lb = sender as ListBox;
// lb.SelectedIndex is also 0
I think I may have found the problem. The ListBox DataValueField is same for a ListBox and the SelectedItem is returned based on the selection of the value.