.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="checkbox遍历.aspx.cs" Inherits="第三章.checkbox遍历" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id="mydiv">
<h2>CheckBox应用</h2>
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" Text="测试"
AutoPostBack="True" /><asp:TextBox ID="txt_test" runat="server"></asp:TextBox>
<%--程序2--%>
<div id="div_chklist" runat="server" >
<p>请选择你的爱好:</p>
<asp:CheckBox ID="CheckBox2" runat="server" Text ="逛街" Width="100px" />
<asp:CheckBox ID="CheckBox3" runat="server" Text ="喝茶" Width="100px" />
<asp:CheckBox ID="CheckBox4" runat="server" Text ="唱歌" Width="100px" />
<asp:CheckBox ID="CheckBox5" runat="server" Text ="购物" Width="100px" />
<asp:CheckBox ID="CheckBox6" runat="server" Text ="看电影" Width="100px" />
<asp:CheckBox ID="CheckBox7" runat="server" Text ="看书" Width="100px" />
<asp:CheckBox ID="CheckBox8" runat="server" Text ="旅游" Width="100px" />
<asp:CheckBox ID="CheckBox9" runat="server" Text ="听音乐" Width="100px" />
<asp:CheckBox ID="CheckBox10" runat="server" Text ="爬山" Width="100px" />
<asp:CheckBox ID="CheckBox11" runat="server" Text ="玩游戏" Width="100px" />
<asp:CheckBox ID="CheckBox12" runat="server" Text ="看综艺节目" Width="100px" />
<asp:CheckBox ID="CheckBox13" runat="server" Text ="学ASP.NET" Width="100px" />
<br /><br />
<asp:Button ID="btn_submit" runat="server" Text="提交" Height="29px"
Width="78px" οnclick="btn_submit_Click" />
<br /><br />
您选择的是:<asp:TextBox ID="TextBox1" runat="server" Height="56px"
Width="310px" TextMode="MultiLine"></asp:TextBox>
</div>
<hr />
<div style="text-align:center; padding:10px; ">
<asp:Button ID="Button1" runat="server" Text="foreach循环语句应用于数组代码示例"
οnclick="Button1_Click" />
</div>
</div>
</form>
</body>
</html>
.aspx.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 第三章
{
public partial class checkbox遍历 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_submit_Click(object sender, EventArgs e)
{
//首先要清空TEXTBOX的内容;以免受上一次操作的影响
TextBox1.Text = "";
CheckBox chk;
<span style="color:#ff0000;">//foreach语句 ctl是循环的自变量不可被更改;所以要引入另一个变量chk(控件类型)</span>
foreach (Control ctl in div_chklist.Controls)
{
if (ctl is CheckBox)
{
<span style="color:#ff0000;"> chk = (CheckBox)ctl;</span>
if (chk.Checked) { TextBox1.Text += chk.Text + ","; }
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
char[] cA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
foreach (char _ca in cA)
{
TextBox1.Text += _ca.ToString() + Convert.ToChar(13);
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
//千万注意要设置checkbox1的autopostback属性为TRUE
txt_test.Text = (CheckBox1.Checked)?"选中状态!":"非选中状态!";
}
}
}
此处千万注意将checkbox1的autopostback属性设置为TRUE!!!!!
效果如下: