The. aspx code is as follows:
<%@ page language= "C #" autoeventwireup= "true" codefile= "Sample 3-06.aspx.cs" inherits= "Sample_3_6"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<link href= "Stylesheet.css" rel= "StyleSheet" type= "Text/css"/>
<body>
<form id= "Form1" runat= "Server" >
<div id= "Mydiv" >
<%--program 1--%>
<div>
<asp:checkbox id= "CheckBox1" runat= "server" Text = "I am AutoPostBack"
autopostback= "true"oncheckedchanged= "Checkbox1_checkedchanged"/>
<asp:textbox id= "txt_1" runat= "Server" ></asp:TextBox>
</div>
<%--program 2--%>
<div id= "div_chklist" runat= "Server" >
<p> Please choose your hobby:</p>
<asp:checkbox id= "CheckBox2" runat= "server" Text = "shopping" width= "100px"/>
<asp:checkbox id= "CheckBox3" runat= "server" Text = "Tea" width= "100px"/>
<asp:checkbox id= "CheckBox4" runat= "server" Text = "singing" width= "100px"/>
<asp:checkbox id= "CHECKBOX5" runat= "server" Text = "shopping" width= "100px"/>
<asp:checkbox id= "CheckBox6" runat= "server" Text = "Watch movie" width= "100px"/>
<asp:checkbox id= "CheckBox7" runat= "server" Text = "read book" width= "100px"/>
<asp:checkbox id= "CheckBox8" runat= "server" Text = "Travel" width= "100px"/>
<asp:checkbox id= "CheckBox9" runat= "server" Text = "Listen to Music" width= "100px"/>
<asp:checkbox id= "CheckBox10" runat= "server" Text = "Mountain climbing" width= "100px"/>
<asp:checkbox id= "CheckBox11" runat= "server" Text = "Play Game" width= "100px"/>
<asp:checkbox id= "CheckBox12" runat= "server" Text = "See variety show" width= "100px"/>
<asp:checkbox id= "CheckBox13" runat= "server" Text = "Learn ASP." Width= "100px"/>
<br/><br/>
<asp:button id= "Btn_submit" runat= "Server" text= "Submit" height= "29px"
Width= "78px" onclick= "Btn_submit_click"/>
<br/><br/>
You have selected: <asp:textbox id= "TextBox1" runat= "Server" height= "56px"
Width= "310px" textmode= "MultiLine" ></asp:TextBox>
</div>
<div style= "Text-align:center; padding:10px; ">
<asp:button id= "Button1" runat= "Server" text= "foreach Loop statement applied to array code example"
onclick= "Button1_Click"/>
</div>
</div>
</form>
</body>
The. Aspx.cs code is as follows:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;public partial class sample_3_6:system.web.ui.page{protected void Page_Load (object sender, EventArgs e) {} protected void Checkbox1_checkedchanged (object sender, EventArgs e) {//c# 3-tuple operator:? Usage of Txt_1.text = (checkbox1.checked)? "Checked status": "Unchecked"; } protected void Btn_submit_click (object sender, EventArgs e) {//the contents of the TextBox must be emptied first, so as not to be affected by the last action TextBox1 . Text = ""; CheckBox chk; The foreach statement ctl is a looping argument that cannot be changed, so introduce another variable, the CHK (control type), of the foreach (Controlled ctl in div_chklist). Controls) {if (CTL is checkbox) {chk = (checkbox) CTL; 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); } }}
CheckBox and Control Foreach Loop