Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

ASP.NET CheckBox Control - Part 12

Suggested Videos:
Part 10 - TextBox Control
Part 11 - RadioButton Control

In this video we will learn about the properties, methods and events of an CheckBox control

CheckBox Control is used, when you want the user to select more than one option from the available choices. For example, the education of a person. A person can have a graduate degree, post graduate degree and a doctrate. In this case the user selects all the 3 checkboxes. Where as a person, may just have a graduate degree, in which case he only selects, the graduate checkbox.
CheckBox control example

Another example, would be when you want the user to select the days of his availability. check box example

In short, if you want to provide the user with more than one option to select from, then choose a check box Control.

Important Properties of the CheckBox Control
Checked - This is a boolean property, that is used to check if the check box is checked or not.
Text - This is a string property used to get or set the text associated with the check box control
TextAlign - right or left. On which side of the check box the text should appear
AutoPostBack - Set this property to true, if you want the webform to be posted immediately when the checked status of the check box changes.

Focus() - Just like TextBox, checkbox also supports, Focus() method. If you want to set the input focus, to a specific checkbox, Call this method for that check box control.

CheckedChanged - This event is fired when the checked status of the check button control is changed.

HTML of the ASPX page, we used in the example
<div style="font-family:Arial">
    <fieldset style="width:350px">
        <asp:CheckBox ID="GraduateCheckBox" Checked="true" Text="Graduate" runat="server" 
            oncheckedchanged="GraduateCheckBox_CheckedChanged" />
        <asp:CheckBox ID="PostGraduateCheckBox" Text="Post Graduate" runat="server" />
        <asp:CheckBox ID="DoctrateCheckBox" Text="Doctrate" runat="server" />
    <br /><br />
    <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />

Code from the CodeBehind file
protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)

protected void Button1_Click(object sender, EventArgs e)
    StringBuilder sbUserChoices = new StringBuilder();
    if (GraduateCheckBox.Checked)
    if (PostGraduateCheckBox.Checked)
        sbUserChoices.Append(", " + PostGraduateCheckBox.Text);
    if (DoctrateCheckBox.Checked)
        sbUserChoices.Append(", " + DoctrateCheckBox.Text);
    Response.Write("Your Selections: " + sbUserChoices.ToString());

protected void GraduateCheckBox_CheckedChanged(object sender, EventArgs e)
    Response.Write("Graduate Checkbox Selection changed");

No comments:

Post a Comment

It would be great if you can help share these free resources