ASP ListBox returns -1
本文关键字:returns ListBox ASP | 更新日期: 2023-09-27 18:09:29
我有一个ASP listBox。每当我在列表中选择一个新项时,将调用以下函数:
protected void prevSubList_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the currently selected item in the ListBox index
int index = prevSubList.SelectedIndex;
if (index < 0)
{
return;
}
//get the nominee for that index
Nominees currNominee = nominees[index];
populateFields(currNominee);
}
<td ID="previousSubmissions" align="left">
<asp:ListBox ID="prevSubList" runat="server" Height="16px" Width="263px"
Rows="1" onselectedindexchanged="prevSubList_SelectedIndexChanged"
AutoPostBack="True">
</asp:ListBox>
</td>
问题是int index = prevSubList.SelectedIndex;
总是求值为-1。我的列表中有三个元素,假设我选择了第二个,我希望它的值是1,但它是-1。知道为什么吗?
您可能正在将数据绑定到Page_Load并且您没有检查IsPostBack是否为真。
的例子:
if (!IsPostBack)
{
Dictionary<string, string> data = new Dictionary<string, string>();
for (int i = 0; i < 10; i++)
data.Add("i" + i, "i" + i);
prevSubList.DataSource = data;
prevSubList.DataBind();
}