2012-11-06 36 views
0

我有一个列表框和一个提交按钮,点击时会触发一个事件。在事件处理程序中,我需要现在选择列表框中哪些元素的信息。问题是每次索引是-1?提交后获得列表框中选定值的值

<form id="form1" runat="server"> 
    <div> 

     <asp:ListBox ID="ListBox1" runat="server" Height="400px" Width="200px"></asp:ListBox> 

    </div> 
     <asp:Button ID="Button1" runat="server" Height="60px" Text="Send" Width="100px" OnClick="send"/> 

    </form> 

这是.aspx文件中的代码。

+0

你能证明你在哪里得到的指数? – tmesser

+1

您确定列表框在回复时不会在您的Page_Load中重新填充? –

回答

0

您可以使用GetSelectedIndices()方法来获取选定的索引。

int[] selectedItemsIndexes = myListbox.GetSelectedIndices(); 

,或者您可以使用

foreach(Object selecteditem in listBox1.SelectedItems) 
{ 

}