Monday, September 20, 2010

CheckBox Server Control

CheckBox Server Control

CheckBox controls user to check the mandatory or optional checkbox depending on the requirement.

CheckBox code: without AutopostBack property. When there is an option that user should select the Terms and conditions before submitting the form.

.aspx

<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" runat="server" Text="I accept the terms and conditions" />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>

.cs

protected void Button1_Click(object sender, EventArgs e)
{
if (CheckBox1.Checked)
{
Label1.Text = "CheckBox1 am checked";
}
else
{
Label1.Text = "Please Accept the terms and conditions to Continue";
}
}

CheckBox code: When user selects the checkbox then an event called.

.aspx

<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" AutoPostBack="true" runat="server"
Text="Donate 10$ to CoolTuts" oncheckedchanged="CheckBox1_CheckedChanged" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>

.cs

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked)
Label1.Text = "Thanks for your donation";
else
Label1.Text = String.Empty;
}

No comments:

Post a Comment