Radio Button

RadioButton Control in ASP.NET Web Forms Using C# – CheckedChanged Event Example

Introduction

Suppose you're creating a form where users need to choose one color from two available options: Red or Blue.

You want the user to select only one color at a time. When they select Red, a TextBox should display Red. When they select Blue, the TextBox should update to Blue.

This is a common situation where the RadioButton control is useful.

In ASP.NET Web Forms, RadioButton controls allow users to choose one option from a group. We can use the CheckedChanged event to respond to the selected option and update another control.

In this tutorial, we'll create two RadioButtons and one TextBox, write the C# event handlers, and understand the role of GroupName and AutoPostBack.

This example uses ASP.NET Web Forms with C# on the .NET Framework.

1. What Is a RadioButton in ASP.NET?

A RadioButton is an ASP.NET server control that allows users to select an option.

RadioButtons are similar to CheckBoxes because both have a Checked property that returns true or false.

However, there is an important difference.

CheckBoxes allow users to select multiple independent options.

RadioButtons, when placed in the same group, allow users to select only one option at a time.

For example:

(●) Red
( ) Blue

Here, Red is selected.

If the user selects Blue, Red is automatically deselected.

Basic RadioButton Syntax

<asp:RadioButton
    ID="RadioButton1"
    runat="server"
    Text="Red" />

2. Difference Between RadioButton and CheckBox

FeatureRadioButtonCheckBox
SelectionOne option per groupMultiple independent options
Checked propertyYesYes
CheckedChanged eventYesYes
AutoPostBackSupportedSupported
GroupNameUsed to group optionsNot applicable
Common useChoosing one color, payment method, or planSelecting hobbies, interests, or preferences

For our example, RadioButton is the correct choice because the user should select either Red or Blue.

3. Create Two RadioButtons and One TextBox

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

Add a new Web Form named RadioButtonExample.aspx.

From the Toolbox, add two RadioButton controls and one TextBox control.

Set the control IDs as follows:

  • RadioButton1 — Red

  • RadioButton2 — Blue

  • TextBox1 — Displays the selected color

Set both RadioButtons' GroupName property to Colors and their AutoPostBack property to True.

4. Complete ASPX Design Code

Open RadioButtonExample.aspx and add:

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

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

        <h2>Select Your Favorite Color</h2>

        <asp:RadioButton
            ID="RadioButton1"
            runat="server"
            Text="Red"
            GroupName="Colors"
            AutoPostBack="true"
            OnCheckedChanged="RadioButton_CheckedChanged" />

        <br /><br />

        <asp:RadioButton
            ID="RadioButton2"
            runat="server"
            Text="Blue"
            GroupName="Colors"
            AutoPostBack="true"
            OnCheckedChanged="RadioButton_CheckedChanged" />

        <br /><br />

        <asp:Label
            ID="lblColor"
            runat="server"
            Text="Selected Color:"
            AssociatedControlID="TextBox1" />

        <br />

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

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

Understanding the ASPX Code

Each RadioButton has a different ID and display text.

The following property groups both controls together:

GroupName="Colors"

Because both RadioButtons belong to the same group, selecting one automatically deselects the other.

The property:

AutoPostBack="true"

allows the browser to submit the page when the selection changes.

Both RadioButtons use the same event handler because they perform the same task.

5. Complete C# Code for RadioButton CheckedChanged

Open the code-behind file RadioButtonExample.aspx.cs.

using System;
using System.Web.UI;

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

        protected void RadioButton_CheckedChanged(
            object sender, EventArgs e)
        {
            if (RadioButton1.Checked)
            {
                TextBox1.Text = "Red";
            }
            else if (RadioButton2.Checked)
            {
                TextBox1.Text = "Blue";
            }
            else
            {
                TextBox1.Text = "";
            }
        }
    }
}

How the Code Works

When the user selects Red, RadioButton1.Checked becomes true.

The event handler executes:

TextBox1.Text = "Red";

When the user selects Blue, RadioButton2.Checked becomes true, and the TextBox displays Blue.

The else block handles the situation where neither RadioButton is selected, such as the initial state of the form.

6. Original Example Using Separate Event Handlers

You can also write separate CheckedChanged methods for each RadioButton, as in the original example.

C# Code

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

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

To use these separate handlers, change the ASPX event attributes:

OnCheckedChanged="RadioButton1_CheckedChanged"

for the first RadioButton, and:

OnCheckedChanged="RadioButton2_CheckedChanged"

for the second.

Both approaches are valid. The shared event handler is easier to maintain when several RadioButtons perform similar actions.

7. What Is GroupName in ASP.NET RadioButton?

The GroupName property is one of the most important RadioButton properties.

It determines which RadioButtons belong to the same selection group.

For example:

<asp:RadioButton
    ID="RadioButton1"
    runat="server"
    Text="Red"
    GroupName="Colors" />

<asp:RadioButton
    ID="RadioButton2"
    runat="server"
    Text="Blue"
    GroupName="Colors" />

Because both controls have GroupName="Colors", only one can be selected at a time.

If you use different group names, the RadioButtons belong to different groups, so both can be selected independently.

8. What Is AutoPostBack in RadioButton?

The AutoPostBack property determines whether selecting a RadioButton automatically submits the page to the server.

Syntax

AutoPostBack="true"

When AutoPostBack is true, the server can process the selection change immediately.

When AutoPostBack is false, the selection change can still be processed during a later postback, such as when a Button is clicked.

For this tutorial, we use AutoPostBack because the TextBox should update as soon as the user selects a color.

9. Output of the RadioButton Example

Initial State

( ) Red
( ) Blue

Selected Color: [             ]

Neither RadioButton is selected, and the TextBox is empty.

When Red Is Selected

(●) Red
( ) Blue

Selected Color: [ Red         ]

When Blue Is Selected

( ) Red
(●) Blue

Selected Color: [ Blue        ]

Notice that selecting Blue automatically deselects Red because both controls belong to the same group.

10. Common Errors and Solutions

Both RadioButtons Can Be Selected

This usually happens when the RadioButtons do not have the same GroupName.

Set both controls to:

GroupName="Colors"

CheckedChanged Does Not Execute Immediately

Check that AutoPostBack="true" is configured and the event handler is correctly connected.

TextBox Is Not Updating

Make sure the code updates the correct control:

TextBox1.Text = "Red";

Also check that other code in Page_Load is not overwriting the TextBox value.

Event Handler Is Not Found

Make sure the method name in the ASPX markup matches the code-behind method.

The Page Reloads When a RadioButton Is Selected

This is expected with AutoPostBack. The browser submits the form to the server, which processes the event and returns the updated page.

For a simple client-side display change that does not require server processing, JavaScript may be more efficient.

11. Best Practices

Use the same GroupName for RadioButtons that represent one set of mutually exclusive options.

Use clear labels so users understand what each option means.

Use AutoPostBack only when immediate server processing is necessary.

Avoid repeating the same logic across several event handlers when a shared handler can perform the task.

For larger groups of options, consider the ASP.NET RadioButtonList control.


Frequently Asked Questions

What is RadioButton in ASP.NET?

RadioButton is a Web Forms server control that allows users to select one option from a group of related choices.

What is the difference between RadioButton and CheckBox?

RadioButtons in the same group allow only one selection, while CheckBoxes allow multiple independent selections.

What is GroupName in RadioButton?

GroupName identifies the group to which a RadioButton belongs. RadioButtons with the same GroupName are mutually exclusive.

What is CheckedChanged in RadioButton?

CheckedChanged is a server-side event raised when ASP.NET detects a change in the RadioButton's checked state during postback processing.

Why do we use AutoPostBack in RadioButton?

AutoPostBack allows the selection change to initiate a server postback immediately.

How do I get the selected RadioButton value in C#?

For individual RadioButtons, check their Checked properties:

if (RadioButton1.Checked)
{
    TextBox1.Text = "Red";
}

Can two RadioButtons use the same CheckedChanged event?

Yes. Multiple RadioButtons can reference the same event handler.

What is the difference between RadioButton and RadioButtonList?

RadioButton represents an individual option, while RadioButtonList manages a collection of related radio options.

Does RadioButton work in ASP.NET Core?

ASP.NET Core supports HTML radio inputs and other form-binding approaches, but it does not support the ASP.NET Web Forms asp:RadioButton server control used in this tutorial.

Conclusion

The RadioButton control is useful whenever users need to select one option from a group.

In this tutorial, we created two RadioButtons for Red and Blue and displayed the selected value in a TextBox.

We also learned about three important concepts:

  • Checked identifies whether a RadioButton is selected.

  • GroupName ensures that only one option in a group can be selected.

  • CheckedChanged and AutoPostBack allow the application to respond to selection changes.

Understanding these properties makes it easier to build interactive forms using ASP.NET Web Forms and C#.

Official Microsoft Documentation

Comments