ASP.NET Checkbox Control


CheckBoxes allow the user to make multiple selections from a number of options and CheckBoxes comes with a caption.

Properties

Property Description
AutoPostBack Specifies whether the form should be posted immediately after the Checked property has changed or not. Default is false
CausesValidation Specifies if a page is validated when a Button control is clicked
Checked Specifies whether the check box is checked or not
InputAttributes Attribute names and values used for the Input element for the CheckBox control
LabelAttributes Attribute names and values used for the Label element for the CheckBox control
runat Specifies that the control is a server control.  Must be set to "server"
Text The text next to the check box
TextAlign On which side of the check box the text should appear (right or left)
ValidationGroup Group of controls for which the Checkbox control causes validation when it posts back to the server
OnCheckedChanged The name of the function to be executed when the Checked property has changed

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckBox1.Text = "ASP.NET";
            CheckBox1.AutoPostBack = true;

        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            Label1.Text = "CheckBox Checked";
        }

       
       
        
    }
}

HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Button Control </title>
</head>
<body>
   
        <form runat="server">
        <asp:CheckBox ID="CheckBox3" runat="server" oncheckedchanged="CheckBox3_CheckedChanged" />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label">
        </form>
   
</body>
</html>

ASP.NET Control Standard Properties

AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableViewState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible
For a full description, go to Web Control Standard Attributes.

ASP.NET Web Control Standard Properties

AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width
For a full description, go to Web Control Standard Attributes.

Example of CheckBox Control

Declare one CheckBox control in an .aspx file.

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckBox1.Text = "CheckBox1";
            CheckBox2.Text = "CheckBox2";
            CheckBox3.Text = "CheckBox3";
            CheckBox4.Text = "CheckBox4";
            CheckBox5.Text = "CheckBox5";
            CheckBox1.AutoPostBack = true;
            CheckBox2.AutoPostBack = true;
            CheckBox3.AutoPostBack = true;
            CheckBox4.AutoPostBack = true;
            CheckBox5.AutoPostBack = true;
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            Label1.Text = "CheckBox1 is Checked";
        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            Label1.Text = "CheckBox2 is Checked";
        }

        protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
        {
            Label1.Text = "CheckBox3 is Checked";
        }

        protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
        {
            Label1.Text = "CheckBox4 is Checked";
        }

        protected void CheckBox5_CheckedChanged(object sender, EventArgs e)
        {
            Label1.Text = "CheckBox5 is Checked";
        }  
        
    }
}

HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Button Control </title>
</head>
<body>
   
        <form runat="server">
        <asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" />
        <br />
        <asp:CheckBox ID="CheckBox2" runat="server" oncheckedchanged="CheckBox2_CheckedChanged" />
        <br />
        <asp:CheckBox ID="CheckBox3" runat="server" oncheckedchanged="CheckBox3_CheckedChanged" />
        <br />
        <asp:CheckBox ID="CheckBox4" runat="server" oncheckedchanged="CheckBox4_CheckedChanged" />
        <br />
        <asp:CheckBox ID="CheckBox5" runat="server" oncheckedchanged="CheckBox5_CheckedChanged" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
   
</body>
</html>

Output

CheckBox Control using JavaScript in HTML Code

Declare one CheckBoxList control in an .aspx file. Then we create an event handler for the SelectedIndexChanged event. The selectable list contains six check boxes. When a user checks one of the boxes, the page is immediately posted back to the server, and the Check subroutine is executed. The subroutine loops through the control's Items collection and tests each item's Selected property. The selected items are then displayed in the Label control.

<script  runat="server">
Sub Check(sender As Object, e As EventArgs)
   dim i
   mess.Text="<p>Selected Items:</p>"
   for i=0 to check1.Items.Count-1
     if check1.Items(i).Selected then
       mess.Text+=check1.Items(i).Text + "<br>"
     end if
   next
End Sub
</script>

<html>
<body>

        <form runat="server">
        <asp:CheckBoxList id="check1" AutoPostBack="True" TextAlign="Right" 
           OnSelectedIndexChanged="Check" runat="server">
        <asp:ListItem> Item 1 </asp:ListItem>
        <asp:ListItem> Item 2 </asp:ListItem>
        <asp:ListItem> Item 3 </asp:ListItem>
        <asp:ListItem> Item 4 </asp:ListItem>
        <asp:ListItem> Item 5 </asp:ListItem>
        <asp:ListItem> Item 6 </asp:ListItem>
        </asp:CheckBoxList>
        <br>
        <asp:label id="mess" runat="server"/>
        </form>

</body>
</html>

Output


Share this article on