首页 > 代码库 > CheckBoxList控件

CheckBoxList控件

 

前台代码:

1 <asp:CheckBoxList ID="CheckBoxList1" runat="server">2     <asp:ListItem Text ="苹果"></asp:ListItem>3     <asp:ListItem Text ="柠檬"></asp:ListItem>4     <asp:ListItem Text ="橘子"></asp:ListItem>5     <asp:ListItem Text ="桃子"></asp:ListItem>6 </asp:CheckBoxList>7 <asp:Button ID="Button1" runat="server" Text="结果" OnClick ="Button1_Click"/>8 <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

 

后台代码:

 1 /// <summary> 2 /// Button单击事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void Button1_Click(object sender, EventArgs e) 7 { 8     string s = string.Empty; 9 10     for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)11     {12         if (this.CheckBoxList1.Items[i].Selected)13         {14             s += this.CheckBoxList1.Items[i].Text + " ";15         }16     }17     this.Label1.Text = "你选择的水果有:" + s;18 }

 

最终效果:

技术分享

 

以上就是CheckBoxList控件的简单用法。

 

CheckBoxList控件