ListBox1控件
前台:
<div> <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="66px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged1" Width="80px"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> <asp:ListItem>4</asp:ListItem> </asp:ListBox> <asp:Label ID="Label1" runat="server"></asp:Label> </div>
后台
第一种foreach循环
protected void ListBox1_SelectedIndexChanged1(object sender, EventArgs e) { string strHobby = ""; foreach (ListItem item in ListBox1.Items) { if (item.Selected) { strHobby += item.Value + ""; } } Label1.Text = strHobby; }
for循环
protected void ListBox1_SelectedIndexChanged1(object sender, EventArgs e) { string strHobby = ""; for (int i = 0; i < ListBox1.Items.Count; i++) { if (ListBox1.Items[i].Selected) { strHobby += ListBox1.Items[i].Value ; } } Label1.Text = strHobby; }