ASP.Net CheckBox Checked Property

Updated on 03 Jul 2012,
Published on 12 Oct 2009

The Checked property of ASP.Net Checkbox control enables you to get or set the checked state of checkbox. You can also set the checked property of checkbox control dynamically using server code or manually when user clicks the checkbox control to change its checked state. The checked property of ASP.Net checkbox control accepts the Boolean value to set its state. It also returns the Boolean value to represent the state of the checkbox control placed on the web page. When user clicks the checkbox control to set its state as checked then it returns "true" and when user un-checks the checkbox control then checked property returns "false" as a resulting value to represent the state of the checkbox control.

CheckBox Control Examples:

You can see the live samples and examples of CheckBox Control from the following links:

Sample Code for ASP.Net Checkbox Checked Property

HTML Code
<asp:CheckBox ID="CheckBox1" 
    runat="server" 
    Text="Checkbox control 1" 
    Checked="True" />
<br />
<br />
<asp:CheckBox ID="CheckBox2" 
    runat="server" 
    Text="Checkbox control 2" />
<br />
<br />
<asp:Button ID="Button1" 
    runat="server" 
    OnClick="Button1_Click" 
    Text="Submit" />
<br />
<br />
<asp:Label ID="Label1" 
    runat="server">
</asp:Label>

In the above HTML source code for Checkbox control we have declared the Checked property as "true" for the first Checkbox control. It will render the checkbox in checked state initially.

C# Code
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        CheckBox2.Checked = true;
}

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Checkbox1.Checked = " + CheckBox1.Checked.ToString() + "<br />";
    Label1.Text += "Checkbox2.Checked = " + CheckBox2.Checked.ToString();
}

In the C# code above we declared the value for Checked property of Checkbox2 dynamically that will render the Checkbox2 in checked state initially. When you will click the button control it will display the value Checked property of both the checkbox controls.

Output:

Get the output for above discussed code from the following link:

CheckBox Checked Property

Continue to the next tutorial: ASP.Net CheckBox AutoPostback Property to learn how to submit the value of checkbox control to the server when user changes the checked state of the checkbox control.

0 Responses to "ASP.Net CheckBox Checked Property"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email