前台代码:
1 <asp:CheckBox ID="CheckBox1" runat="server" Text ="苹果"/> 2 <asp:CheckBox ID="CheckBox2" runat="server" Text ="柠檬"/> 3 <asp:CheckBox ID="CheckBox3" runat="server" Text ="橘子"/> 4 <asp:CheckBox ID="CheckBox4" runat="server" Text ="桃子"/> 5 <asp:Button ID="Button2" runat="server" Text="结果" OnClick ="Button2_Click"/> 6 <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
后台代码:
1 /// <summary> 2 /// Button单击事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void Button2_Click(object sender, EventArgs e) 7 { 8 string s = string.Empty; 9 10 if (this.CheckBox1.Checked) 11 { 12 s += this.CheckBox1.Text + " "; 13 } 14 if (this.CheckBox2.Checked) 15 { 16 s += this.CheckBox2.Text + " "; 17 } 18 if (this.CheckBox3.Checked) 19 { 20 s += this.CheckBox3.Text + " "; 21 } 22 if (this.CheckBox4.Checked) 23 { 24 s += this.CheckBox4.Text + " "; 25 } 26 27 this.Label2.Text = "你选择的水果有:" + s; 28 }
最终效果;
以上就是CheckBox控件的用法。