CheckBox Event Program

How to Use Multiple CheckBox CheckedChanged Events in ASP.NET Web Forms Using C#

Introduction

Suppose you're creating a simple form where users can select their favorite colors. You provide three options: Red, Blue, and Yellow.

When a user selects Red, you want the TextBox to display Red. If they also select Blue, the TextBox should show both selected colors. When they uncheck Red, only Blue should remain.

This is a useful example for understanding how multiple CheckBox controls work in ASP.NET Web Forms.

In this tutorial, we'll create three CheckBoxes and one TextBox, handle their CheckedChanged events, and display the selected values using C#.

We'll also look at a common mistake that occurs when multiple CheckBoxes update the same TextBox.

This tutorial applies to ASP.NET Web Forms on the .NET Framework.

1. Understanding Multiple CheckBoxes in ASP.NET

A CheckBox allows users to select or deselect an option independently.

Unlike RadioButton controls, multiple CheckBoxes can be checked at the same time.

For example:

[x] Red
[x] Blue
[ ] Yellow

In this case, both Red and Blue are selected.

Each CheckBox has a Checked property that returns either true or false.

CheckBox1.Checked
CheckBox2.Checked
CheckBox3.Checked

We can use these properties to determine which colors are currently selected.

2. What Is the CheckedChanged Event?

The CheckedChanged event is raised when ASP.NET detects that a CheckBox's checked state has changed during postback processing.

For example, selecting Red can execute:

protected void CheckBox1_CheckedChanged(
    object sender, EventArgs e)
{
    // Handle the selection change.
}

To execute the server-side event immediately after the user changes a selection, set:

AutoPostBack="true"

Without AutoPostBack, the event can still be processed during a later postback.

3. Create a Web Form with Three CheckBoxes

Open Visual Studio and create or open an ASP.NET Web Forms project targeting the .NET Framework.

Add a Web Form named MultipleCheckBox.aspx.

We'll use four controls:

  • CheckBox1 for Red.

  • CheckBox2 for Blue.

  • CheckBox3 for Yellow.

  • TextBox1 to display the selected colors.

4. Complete ASPX Design Code

Open MultipleCheckBox.aspx and add the following markup.

<%@ Page Language="C#"
    AutoEventWireup="true"
    CodeBehind="MultipleCheckBox.aspx.cs"
    Inherits="WebFormsDemo.MultipleCheckBox" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Multiple CheckBox Example</title>
</head>
<body>
    <form id="form1" runat="server">

        <h2>Select Your Favorite Colors</h2>

        <asp:CheckBox
            ID="CheckBox1"
            runat="server"
            Text="Red"
            AutoPostBack="true"
            OnCheckedChanged="CheckBox1_CheckedChanged" />

        <br /><br />

        <asp:CheckBox
            ID="CheckBox2"
            runat="server"
            Text="Blue"
            AutoPostBack="true"
            OnCheckedChanged="CheckBox2_CheckedChanged" />

        <br /><br />

        <asp:CheckBox
            ID="CheckBox3"
            runat="server"
            Text="Yellow"
            AutoPostBack="true"
            OnCheckedChanged="CheckBox3_CheckedChanged" />

        <br /><br />

        <asp:Label
            ID="lblSelectedColors"
            runat="server"
            Text="Selected Colors:"
            AssociatedControlID="TextBox1" />

        <br />

        <asp:TextBox
            ID="TextBox1"
            runat="server"
            ReadOnly="true" />

    </form>
</body>
</html>

Explanation

Each CheckBox has its own CheckedChanged event handler.

All three CheckBoxes have AutoPostBack="true" so the page updates when the user changes a selection.

The TextBox is read-only because it displays the selected colors rather than accepting typed input.

5. Basic Example: Display One Color at a Time

Let's first understand the approach used in the original example.

protected void CheckBox1_CheckedChanged(
    object sender, EventArgs e)
{
    if (CheckBox1.Checked)
    {
        TextBox1.Text = "Red";
    }
    else
    {
        TextBox1.Text = "";
    }
}

protected void CheckBox2_CheckedChanged(
    object sender, EventArgs e)
{
    if (CheckBox2.Checked)
    {
        TextBox1.Text = "Blue";
    }
    else
    {
        TextBox1.Text = "";
    }
}

protected void CheckBox3_CheckedChanged(
    object sender, EventArgs e)
{
    if (CheckBox3.Checked)
    {
        TextBox1.Text = "Yellow";
    }
    else
    {
        TextBox1.Text = "";
    }
}

This works when the user selects just one CheckBox at a time.

However, there's a problem when more than one CheckBox is selected.

For example, if the user checks Red and then Blue, the TextBox displays Blue, replacing Red.

If the user then unchecks Blue, the TextBox becomes empty even though Red is still checked.

That's because each event handler updates the TextBox without considering the state of the other CheckBoxes.

Let's fix this.

6. Improved Example: Display All Selected Colors

Instead of updating the TextBox separately in each event handler, we'll create one method that checks all three CheckBoxes.

This method will build a list of selected colors and display them together.

Complete C# Code

Open MultipleCheckBox.aspx.cs.

using System;
using System.Collections.Generic;
using System.Web.UI;

namespace WebFormsDemo
{
    public partial class MultipleCheckBox : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void CheckBox1_CheckedChanged(
            object sender, EventArgs e)
        {
            UpdateSelectedColors();
        }

        protected void CheckBox2_CheckedChanged(
            object sender, EventArgs e)
        {
            UpdateSelectedColors();
        }

        protected void CheckBox3_CheckedChanged(
            object sender, EventArgs e)
        {
            UpdateSelectedColors();
        }

        private void UpdateSelectedColors()
        {
            List<string> selectedColors =
                new List<string>();

            if (CheckBox1.Checked)
            {
                selectedColors.Add("Red");
            }

            if (CheckBox2.Checked)
            {
                selectedColors.Add("Blue");
            }

            if (CheckBox3.Checked)
            {
                selectedColors.Add("Yellow");
            }

            TextBox1.Text =
                string.Join(", ", selectedColors);
        }
    }
}

How This Code Works

Every time the user changes a CheckBox selection, its event handler calls:

UpdateSelectedColors();

Inside this method, we create a list:

List<string> selectedColors =
    new List<string>();

Next, we check the state of each CheckBox.

If Red is checked, we add Red to the list.

If Blue is checked, we add Blue.

If Yellow is checked, we add Yellow.

Finally, we combine the selected values:

TextBox1.Text =
    string.Join(", ", selectedColors);

If Red and Blue are selected, the TextBox displays:

Red, Blue

If all three CheckBoxes are selected, the TextBox displays:

Red, Blue, Yellow

If none are selected, the TextBox becomes empty.

7. Output of the Multiple CheckBox Example

The output depends on which CheckBoxes are selected.

RedBlueYellowTextBox Output
UncheckedUncheckedUncheckedEmpty
CheckedUncheckedUncheckedRed
UncheckedCheckedUncheckedBlue
UncheckedUncheckedCheckedYellow
CheckedCheckedUncheckedRed, Blue
CheckedUncheckedCheckedRed, Yellow
CheckedCheckedCheckedRed, Blue, Yellow

This behavior is more appropriate when the user can select multiple options.

8. Can We Use One CheckedChanged Event for All Three CheckBoxes?

Yes. You don't need three separate event handlers if they all perform the same action.

You can connect all three CheckBoxes to one shared handler.

ASPX Example

<asp:CheckBox
    ID="CheckBox1"
    runat="server"
    Text="Red"
    AutoPostBack="true"
    OnCheckedChanged="Color_CheckedChanged" />

<asp:CheckBox
    ID="CheckBox2"
    runat="server"
    Text="Blue"
    AutoPostBack="true"
    OnCheckedChanged="Color_CheckedChanged" />

<asp:CheckBox
    ID="CheckBox3"
    runat="server"
    Text="Yellow"
    AutoPostBack="true"
    OnCheckedChanged="Color_CheckedChanged" />

C# Code

protected void Color_CheckedChanged(
    object sender, EventArgs e)
{
    UpdateSelectedColors();
}

The UpdateSelectedColors() method remains the same as in the previous example.

This approach reduces repeated code and makes maintenance easier.

9. Checked vs CheckedChanged vs AutoPostBack

These three concepts are closely related but serve different purposes.

FeatureTypePurpose
CheckedBoolean propertyIdentifies whether a CheckBox is selected
CheckedChangedServer-side eventResponds to changes in the checked state
AutoPostBackBoolean propertyAutomatically submits the page when the checked state changes

For example:

if (CheckBox1.Checked)
{
    // Red is selected.
}

checks the current state.

The CheckedChanged handler runs when ASP.NET processes a change.

AutoPostBack="true" initiates the postback immediately after the user changes the CheckBox.

10. Common Problems and Solutions

Problem: Only the Last Selected Color Appears

This happens when every event handler directly replaces TextBox1.Text.

Solution: Check all three CheckBoxes and combine their selected values.

Problem: Unchecking One CheckBox Clears Everything

This happens when an event handler assigns an empty string without checking the other selections.

Solution: Rebuild the TextBox value from the current checked states.

Problem: CheckedChanged Does Not Execute Immediately

Check whether AutoPostBack="true" is configured.

Also verify that the event handler name in the ASPX page matches the C# method.

Problem: The TextBox Value Is Reset Unexpectedly

Check whether Page_Load or another event handler overwrites the TextBox value on every postback.

Avoid unnecessary assignments during page initialization.

Problem: The Page Reloads After Each Selection

This is expected when using AutoPostBack.

For simple browser-side updates that do not require server processing, JavaScript can provide a smoother experience without a full postback.

11. Practical Uses of Multiple CheckBoxes

The same technique can be used in many real-world applications.

For example, you might allow users to select multiple hobbies, preferred communication channels, product features, or skills.

The application can collect the selected options and display them in a TextBox, Label, or summary section.

For larger groups of related options, ASP.NET Web Forms also provides the CheckBoxList control, which can be easier to manage than several individual CheckBoxes.


Frequently Asked Questions

How do I use multiple CheckBoxes in ASP.NET Web Forms?

Add multiple asp:CheckBox controls to the page and read their Checked properties in C#.

Can more than one CheckBox be checked at a time?

Yes. CheckBoxes allow independent selections.

How do I display multiple selected CheckBox values in a TextBox?

Check each control's Checked property, collect the selected values, and combine them using string.Join().

What is the use of AutoPostBack in CheckBox?

It allows the page to post back automatically when the checked state changes, so the server-side event handler can run immediately.

Can multiple CheckBoxes use the same CheckedChanged event?

Yes. Multiple CheckBox controls can reference the same event handler.

What is the difference between CheckBox and CheckBoxList?

A CheckBox represents one independent selection. A CheckBoxList manages a collection of related selectable items.

What happens when all CheckBoxes are unchecked?

In our improved example, the selected-colors list becomes empty, so the TextBox displays an empty string.

Does this example work in ASP.NET Core?

No. This example uses ASP.NET Web Forms server controls, which are part of the .NET Framework.

Conclusion

Using multiple CheckBox controls in ASP.NET Web Forms is a simple way to collect several selections from a user.

In this tutorial, we created three CheckBoxes for Red, Blue, and Yellow and displayed the selected values in a TextBox.

We also improved the original approach by using one reusable method to handle multiple selections correctly.

The key idea is to check the current state of every CheckBox instead of updating the TextBox based only on the control that triggered the event.

This makes the code more reliable, easier to understand, and easier to extend.

Official Microsoft Documentation


Comments