ASP.NET Web Forms CheckBox Control: Implementation and Usage

Learn how to implement and utilize the CheckBox control in ASP.NET Web Forms. This tutorial covers creating checkboxes, handling multiple selections, customizing properties, and processing selected values on the server. Build interactive web forms with ease.



ASP.NET Web Forms CheckBox Control

Introduction to the CheckBox Control

The CheckBox control in ASP.NET Web Forms allows users to select one or more options from a set of choices. It provides a simple yes/no or true/false selection mechanism. It's a server-side control, meaning it's processed on the server, and the selected values are sent to the server when the form is submitted. You can add a CheckBox control to your form using the Visual Studio drag-and-drop interface or by writing the code directly into your ASPX file.

Creating a CheckBox Control

Here's how to create a CheckBox control in ASP.NET:

Syntax

<asp:CheckBox ID="CheckBox2" runat="server" Text="J2EE" />
Rendered HTML Output

<input id="CheckBox2" type="checkbox" name="CheckBox2">J2EE

CheckBox Control Properties

The CheckBox control has several properties for customization:

Property Description
AccessKey Sets a keyboard shortcut.
TabIndex Specifies the tab order.
BackColor Sets the background color.
BorderColor Sets the border color.
BorderWidth Sets the border width.
Font Sets the font style.
ForeColor Sets the text color.
Text Sets the text displayed next to the checkbox.
ToolTip Text displayed on hover.
Visible Controls visibility.
Height Sets the height of the checkbox.
Width Sets the width of the checkbox.
Checked Specifies the initial checked state (true/false).

Example: Handling Multiple CheckBox Selections

This example demonstrates how to handle multiple checkbox selections. When the user clicks a button, the code checks which checkboxes are selected and displays them in a label.

ASPX Code (WebControls.aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebControls.aspx.cs" Inherits="WebFormsControlls.WebControls" %>
<form id="form1" runat="server">
    <asp:CheckBox ID="CheckBox1" runat="server" Text="C#"/>
    <asp:CheckBox ID="CheckBox2" runat="server" Text="J2EE"/>
    <asp:CheckBox ID="CheckBox3" runat="server" Text="Python"/><br />
    <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /><br />
    <asp:Label ID="ShowCourses" runat="server"></asp:Label>
</form>

C# CodeBehind (WebControls.aspx.cs)

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFormsControlls
{
public partial class WebControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    ShowCourses.Text = "None";
}
protected void Button1_Click(object sender, EventArgs e)
{
    string message = "";
    if (CheckBox1.Checked) message += CheckBox1.Text + " ";
    if (CheckBox2.Checked) message += CheckBox2.Text + " ";
    if (CheckBox3.Checked) message += CheckBox3.Text;
    ShowCourses.Text = message;
}
}
}
Output

(A screenshot showing the checkboxes and the label displaying the selected options after the button click would be included here.)