开发者

ASP ListBox returns -1

I have a an ASP listBox. Whenever I select a new item in the list, the following function gets called:

 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" 
     AutoPostBa开发者_JAVA技巧ck="True">
     </asp:ListBox>
</td>

The problem is int index = prevSubList.SelectedIndex; always evaluates to -1. There are three items in my list, say I select the second one, I would expect the value to be 1, but it is -1. Any ideas why?


You are probably binding the data on Page_Load and you are not checking whether IsPostBack is true.

Example:

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜