解法一:使用LINQ取出Selected == true
解法二:使用for迴圈尋訪ListBox的每一個ListItem,並判斷ListItem.Selected == true
Sample.aspx
<asp:ListBox ID="ListBox1" runat="server" Rows="5" SelectionMode="Multiple" Width="150px"> <asp:ListItem>AAAA</asp:ListItem> <asp:ListItem>BBBB</asp:ListItem> <asp:ListItem>CCCC</asp:ListItem> <asp:ListItem>DDDD</asp:ListItem> </asp:ListBox>
解法一:
Sample.aspx.csvar selItems = from ListItem li in ListBox1.Items where li.Selected == true select li.Text; Response.Write("Selected Item(s): "); foreach (var item in selItems) { Response.Write(item.ToString() + ""); }
解法二:
Sample.aspx.csListBox1.SelectionMode = ListSelectionMode.Multiple; for (int i = 0; i < ListBox1.Items.Count; i++) { if(ListBox1.Items[i].Selected == true) { Response.Write( ListBox1.Items[i].ToString() + ""); } }
沒有留言:
張貼留言