Table of contents
1.
Introduction
2.
CheckBoxes in ASP.NET
3.
CheckBoxList
4.
Making CheckBox using GUI
5.
CheckBox coding example
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Web Forms CheckBox

Author Toohina Barua
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When you visit a site, it asks you to check the CheckBox saying that you agree to the website's terms and conditions and policies before moving further. In Todo lists, you have CheckBoxes. You tick off the CheckBox corresponding to that task when you complete a task. As you keep completing tasks, you keep checking the CheckBoxes out. So, thus you can check multiple boxes at the same time. Similarly, we can make CheckBoxes for web forms in ASP.NET and achieve this functionality. From this article, we will learn about CheckBoxes and how to make them in ASP.NET.

Source

CheckBoxes in ASP.NET

The CheckBox class shows a check box that allows users to choose whether a condition is true or false.

C# code:

[System.Web.UI.ControlValueProperty("Checked")]
public class CheckBox : System.Web.UI.WebControls.WebControl, System.Web.UI.ICheckBoxControl, System.Web.UI.IPostBackDataHandler

On web forms, the CheckBox control resembles squares. When you have to change the condition to true or false, you check or uncheck the box, respectively. 

Source

CheckBox Syntax:

<asp:CheckBox ID=“CheckBox1“ runat=“server“ />

CheckBoxList

We use CheckBoxList to get multiple inputs from the user. It allows the user to select choices from the set of choices. The CheckBoxList control is an alternate control if you want to use several CheckBox controls.

Source

Making CheckBox using GUI

The Demo web form's initial step is to open the Forms Designer. You'll be able to drag controls from the toolbox to the Web form after that.
To access the Designer web form, right-click the Demo.aspx file in Solution Explorer and select View Designer from the menu.

You will see your Form Designer as shown below.

As seen below, drag the CheckBox control from the toolbox onto the Web Form.

By clicking on the CheckBox control, you'll be sent to the properties panel. Change the appropriate controls' ID properties to 'chkC' and 'chkASP'. Change the CheckBox control's text attribute to 'C#' as well. Change the value of the other CheckBox control to 'ASP.NET'.

Change the text property.

 

After you've made the modifications above, you'll get the following result.

CheckBox coding example

Following is a coding example of how to make CheckBoxes. 

aspx file:

First, we have two individual CheckBoxes for the selection of multiple courses. Then we have a single CheckBox to opt for online. When a user clicks or deselects a CheckBox, you can call server-side c# code (postback). To do so, select AutoPostBack="true" and add OnCheckedChanged="handleCheckedChanged".

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        You can select multiple courses:<br />
        <asp:CheckBox ID="aspnet" runat="server" Text="Asp.net" /><br />
        <asp:CheckBox ID="csharp" runat="server" Text="CSharp" /><br />
        <br />
        <br />
        CheckBox postback event:<br />
        <asp:CheckBox ID="opt_online" runat="server" Text="Opt for online tutorial" AutoPostBack="true"
            OnCheckedChanged="handleCheckedChanged" />
        <br />
        <br />
        <asp:Label ID="displayMessage" runat="server" Style="color: Green; font-size: 18px; font-weight: bold;"></asp:Label>
    </div>
    </form>
</body>
</html>


cs file:

When the CheckBox with id opt_online gets selected or deselected, that is, if there is a change in opt_online value, the handleCheckedChanged method will be called. If the opt_online is ticked, the message will be displayed; otherwise, not. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void handleCheckedChanged(object sender, EventArgs e)
    {
        if (opt_online.Checked == true)
            displayMessage.Text = "You have opted for online tutorial.";
        else
            displayMessage.Text = "";
    }
}

Output:

FAQs

  1. From where did the CheckBox class in ASP.NET inherit?
    The inheritance of CheckBox can be explained as follows:
    Object→ Control→WebControl→CheckBox
     
  2. When should we use CheckBoxList control over multiple CheckBox controls?
    We should choose CheckBoxList control over multiple CheckBox controls when we want convenient data-binding capabilities.
     
  3. When do we prefer using several individual CheckBox controls over CheckBoxList control?
    Individual CheckBox controls are preferred over CheckBoxList control when we want greater control over the layout.
     
  4. What is the difference between the contents of the aspx and cs files?
    Dynamic code (in this case, C# due to the .cs suffix on the file name) is usually placed in the .cs file as a "best practice" to keep dynamic code and static html separate. HTML, CSS, javascript, and other client-side controls are usually put in.aspx files.
     
  5. What does the Checked property of CheckBox do?
    Checked property is used to set the check state of the CheckBox control to either true or false.

Key Takeaways

From this article, we learned about CheckBox and CheckBoxList in ASP.NET. We saw how we could code it through a practical example. 
But this is not enough; you need something extra to excel in web development truly. If you want to learn more about web development, you can read our articles or take our highly curated Web Development course.

Live masterclass