2011-09-21 8 views
0

ASP listBox가 있습니다. 나는이 목록에 새 항목을 선택할 때마다 다음과 같은 함수가 호출됩니다 : 문제는 int index = prevSubList.SelectedIndex;입니다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> 

는 항상 -1로 평가합니다. 내 목록에 세 가지 항목이 있습니다. 두 번째 항목을 선택하면 값이 1이지만 -1이라고 예상됩니다. 어떤 아이디어?

+0

마크 업주세요. – Icarus

+0

@lcarus updated – user489041

+0

값을 다시 설정하거나 모든 포스트 백에서 목록을 지울 수 있습니까? – Kobi

답변

2

아마도 Page_Load에 데이터를 바인딩하고 IsPostBack이 true인지 확인하지 않습니다.

예 :

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(); 
} 
관련 문제