2011-09-21 59 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(); 
}