CHECKBOX

CheckBox CheckedChanged Event in ASP.NET Web Forms Using C# – AutoPostBack Example

Introduction

Suppose you're creating a form where users can select an option using a CheckBox. When they select it, you want to display a value in a TextBox. When they remove the selection, the TextBox should become empty.

For example, imagine a form with a CheckBox labeled Red and a TextBox. When the user checks the box, the TextBox displays "Red". When the user unchecks it, the TextBox is cleared.

In ASP.NET Web Forms, we can build this functionality using the CheckedChanged event and the AutoPostBack property.

This is a simple example, but it helps you understand how server controls respond to user actions.

1. What Is a CheckBox in ASP.NET?

A CheckBox is an ASP.NET Web Forms server control that allows users to select or deselect an option.

It has two possible states:

  • Checked (true) — The option is selected.

  • Unchecked (false) — The option is not selected.

The state is represented by the Checked property.

Basic Syntax

<asp:CheckBox
    ID="CheckBox1"
    runat="server"
    Text="Red" />

In C#, you can check whether the CheckBox is selected using:

if (CheckBox1.Checked)
{
    // Execute when checked
}

The Checked property returns a Boolean value, either true or false.

2. What Is the CheckedChanged Event?

The CheckedChanged event is a server-side event that ASP.NET raises when it detects that the CheckBox's checked state has changed during postback processing.

For example, the event can execute when the user checks or unchecks the CheckBox.

Event Syntax

protected void CheckBox1_CheckedChanged(
    object sender, EventArgs e)
{
    // Code to execute when checked state changes
}

The sender parameter identifies the control that raised the event, while e contains event information.

Understanding Event Handling

Your original notes describe three activities: event, listener, and perform activity.

In ASP.NET Web Forms, we can understand this more precisely:

  1. User action: The user checks or unchecks the CheckBox.

  2. Event handling: ASP.NET detects the changed state during postback processing and invokes the registered event handler.

  3. Code execution: The event handler performs an action, such as updating a TextBox.

3. What Is AutoPostBack in CheckBox?

The AutoPostBack property controls whether changing the CheckBox automatically submits the page to the server.

Syntax

AutoPostBack="true"

When AutoPostBack is true, checking or unchecking the control initiates a postback, allowing the server-side event handler to run immediately.

When AutoPostBack is false, changing the CheckBox does not automatically submit the page. The CheckedChanged event can still be processed during a later postback, such as when the user clicks a Button.

For our example, we'll use AutoPostBack="true" so the TextBox updates as soon as the user changes the CheckBox selection.

4. Step-by-Step Example: Display Red in a TextBox When CheckBox Is Checked

Let's create a Web Forms page containing a CheckBox and a TextBox.

Step 1: Create the ASP.NET Web Form

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

Add a new Web Form named CheckBoxExample.aspx.

Step 2: Add the Controls

Open the Toolbox and drag the following controls onto the page:

  • CheckBox

  • TextBox

Set their IDs to CheckBox1 and TextBox1.

Step 3: Configure AutoPostBack

Select CheckBox1 in the Visual Studio designer and press F4 to open the Properties Window.

Find the AutoPostBack property and set it to True.

Step 4: Create the CheckedChanged Event

In the Properties Window, open the Events section and locate CheckedChanged.

Create an event handler named CheckBox1_CheckedChanged.

Alternatively, define the event handler directly in ASPX markup.

5. Complete ASPX Code

Open CheckBoxExample.aspx and add the following code:

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

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

        <h2>CheckBox CheckedChanged Example</h2>

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

        <br /><br />

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

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

Explanation

CheckBox1 allows the user to select or deselect Red.

AutoPostBack="true" triggers a postback when the checked state changes.

OnCheckedChanged connects the control to the C# event handler.

TextBox1 displays the result. We use ReadOnly="true" because the TextBox is intended to show a value rather than accept user input.

6. Complete C# Code for CheckedChanged

Open CheckBoxExample.aspx.cs and add:

using System;
using System.Web.UI;

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

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

How This Code Works

The following condition checks whether the CheckBox is selected:

if (CheckBox1.Checked == true)

If the condition is true, the TextBox displays Red:

TextBox1.Text = "Red";

Otherwise, the TextBox is cleared:

TextBox1.Text = "";

You can also simplify the event handler:

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

Both versions produce the same result.

7. Output of the CheckBox Example

When the CheckBox Is Unchecked

[ ] Red

TextBox: [                  ]

The TextBox is empty.

When the CheckBox Is Checked

[x] Red

TextBox: [ Red              ]

The TextBox displays Red.

When the User Unchecks It Again

[ ] Red

TextBox: [                  ]

The TextBox becomes empty again.

8. How the CheckedChanged Event Works Internally

Let's understand the flow:

User Opens Web Form
         |
         v
User Checks CheckBox
         |
         v
AutoPostBack = true
         |
         v
Browser Submits Form
         |
         v
ASP.NET Processes Postback
         |
         v
CheckedChanged Event Executes
         |
         v
CheckBox1.Checked == true?
         |
      Yes / No
       /     \
      v       v
  Show Red   Clear TextBox

This demonstrates how ASP.NET Web Forms server-side event handling works.

9. Difference Between Checked and CheckedChanged

These two terms are related but have different purposes.

FeatureCheckedCheckedChanged
TypePropertyEvent
PurposeGets or sets selection stateHandles changes in selection state
Valuetrue or falseEvent handler
ExampleCheckBox1.CheckedCheckBox1_CheckedChanged

Use Checked when you need to determine whether the control is selected.

Use CheckedChanged when you need to execute code after ASP.NET detects a change in the checked state.

10. Common Errors and Solutions

CheckedChanged Does Not Execute Immediately

Make sure the CheckBox has:

AutoPostBack="true"

Also verify that the OnCheckedChanged attribute points to the correct event handler.

TextBox Value Does Not Change

Check that you're updating the correct TextBox ID.

For example:

TextBox1.Text = "Red";

Also ensure that other code, such as unconditional assignments in Page_Load, is not overwriting the result.

Event Handler Is Not Found

Verify that the event handler name in the ASPX markup matches the method in the code-behind file.

The Page Reloads When the CheckBox Is Clicked

This is expected with AutoPostBack="true".

The browser submits the form to the server and ASP.NET returns the updated page.

For purely visual client-side interactions that do not require server processing, JavaScript may be a better choice.

11. Best Practices

Use AutoPostBack when a server-side action needs to happen immediately after the CheckBox state changes.

For simple visual changes, consider JavaScript to avoid an unnecessary server request.

Use meaningful IDs in real applications, such as chkRed and txtSelectedColor.

Keep event handlers simple and avoid unnecessary processing.

When a TextBox is only displaying a result, consider whether a Label would be more appropriate.

For accessibility, ensure the CheckBox has a clear text label so users understand what selecting it will do.


Frequently Asked Questions

What is a CheckBox in ASP.NET?

A CheckBox is a Web Forms server control that allows users to select or deselect an option.

What is the CheckedChanged event?

It is a server-side event that occurs when ASP.NET detects a change in the CheckBox's checked state during postback processing.

Why is AutoPostBack used with CheckBox?

AutoPostBack allows the CheckBox to submit the page automatically when the selection changes, so the server-side event can run immediately.

Is AutoPostBack required for CheckedChanged?

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

How do I check whether a CheckBox is selected in C#?

Use:

if (CheckBox1.Checked)
{
    // Checked
}

How do I display text in a TextBox when CheckBox is checked?

Use:

TextBox1.Text = CheckBox1.Checked ? "Red" : "";

inside the CheckedChanged event handler.

What is the difference between CheckBox and RadioButton?

A CheckBox represents an independent on/off selection. RadioButtons are commonly grouped so users can choose one option from a set.

Does this example work in ASP.NET Core?

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

Conclusion

The CheckBox CheckedChanged event is a useful feature in ASP.NET Web Forms that allows developers to respond when users select or deselect an option.

In this example, we created a CheckBox and a TextBox. When the user checks the box, the TextBox displays Red. When the user unchecks it, the TextBox becomes empty.

The two key concepts are:

CheckBox1.Checked

which returns the CheckBox state, and:

AutoPostBack="true"

which enables an immediate server postback when the state changes.

Understanding these concepts helps you build interactive forms using ASP.NET Web Forms and C#.

Official Microsoft Documentation


Comments