ASP.NET DropDownList - GetSelectedIndices missing one selected item
Maybe I'm losing my mind - I thought this was straight forward.
ListCode.DataTextField = "code_desc";
ListCode.DataValueField = "code_id";
ListCode.DataSource = Foo.GetCodes();
ListCode.DataBind();
The selection mode is set to multipls and all is good, about 50 items with appropriate values displays. Then I select 5 items and submit the form. I do a
int[] indices = ListCode.GetSelectedIndices();
and the array only has the first four items that I selected. It seems that if I select multiple items in the list and submit the form, I'm only able开发者_运维知识库 to retrieve all but the last selected item - it doesn't matter if I use GetSelectedIndices or if I iterate through each item in the list.
Any help would be greatly appreciated.
You must be using a ListBox
I assume. I made the following test app:
<asp:ListBox ID="lstTest" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="Test1" Value="1" />
<asp:ListItem Text="Test2" Value="2" />
<asp:ListItem Text="Test3" Value="3" />
<asp:ListItem Text="Test4" Value="4" />
</asp:ListBox>
<asp:Button ID="btnTest" runat="server" Text="Test" OnClick="btnTest_Click" />
<asp:Label ID="lblTest" runat="server" />
protected void btnTest_Click(object sender, System.EventArgs e)
{
int[] selectedIndexes = lstTest.GetSelectedIndices();
lblTest.Text = selectedIndexes.Length.ToString();
}
Seems to work fine so I have to assume it has something to do with your binding or when you are fetching the indicies. Can you post a trimmed down version of your broken code?
Not really an answer, but I swapped in a checkboxlist and it worked like a champ.
精彩评论