2011-09-21 54 views
0

我有一个ASP列表框。每当我在列表中选择一个新的项目,下面的函数被调用:ASP ListBox返回-1

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。任何想法为什么?

+0

标记,请。 – Icarus

+0

@lcarus已更新 – user489041

+0

是否有可能在每次回传中重置值或清除列表? – Kobi

回答

2

您可能会绑定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(); 
}