Aspx:
<Div>
<Asp: CheckBox ID = "cbAll" runat = "server" AutoPostBack = "True" OnCheckedChanged = "cbAll_CheckedChanged" Text = "select all"/>
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "Button"/> <br/>
</Div>
Aspx. cs:
Page_Load:
// Dynamically Add a CheckBox
Int j = 5;
For (int I = 1; I <= j; I ++)
{
CheckBox cb = new CheckBox ();
Cb. Text = I. ToString ();
Cb. Font. Size = System. Web. UI. WebControls. FontUnit. Small;
Cb. ID = "cb" + I. ToString ();
Cb. Checked = false;
Page. Form. Controls. Add (cb );
}
// Select and cancel all
Protected void cbAll_CheckedChanged (object sender, EventArgs e)
{
CheckBox all = sender as CheckBox;
Foreach (Control ctl in Page. Form. Controls)
{
If (ctl is CheckBox)
{
CheckBox chk = ctl as CheckBox;
Chk. Checked = all. Checked;
}
}
}
// Obtain all selected items.
Protected void button#click (object sender, EventArgs e)
{
Foreach (Control ctl in Page. Form. Controls)
{
If (ctl is CheckBox)
{
CheckBox chk = ctl as CheckBox;
If (chk. Checked = true & chk. Text. Trim ()! = "Select all ")
{
Response. Write (chk. Text );
}
}
}
}