How to get selected value of Radio button in Asp.net using C#

How to get selected value of Radio button in C#. In this C# tutorial, we will learn How to get the selected value of the Radio button in Asp.net using C# .

What is Radio Button?

A radio button or option button enables the user to select a single option from a group of choices when paired with other RadioButton controls. When a user clicks on a radio button, it becomes checked, and all other radio buttons with the same group become unchecked.

The RadioButton control can display text, an image, or both. Use the Checked property to get or set the state of a RadioButton.

radioButton1.Checked = true;

The radio button and the check box are used for different functions. Use a radio button when you want the user to choose only one option. When you want the user to choose all appropriate options, use a check box. Like checkboxes, radio buttons support a Checked property that indicates whether the radio button is selected or not.

C# code to get selected value of Radio button

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                MessageBox.Show ("You are selected Red !! ");
                return;
            }
            else if (radioButton2.Checked == true)
            {
                MessageBox.Show("You are selected Blue !! ");
                return;
            }
            else
            {
                MessageBox.Show("You are selected Green !! ");
                return;
            }
        }
    }
}

Radio button group or list:

We can use one more ideology to do this you can put a group name on regular radio buttons if you have to check which one is selected and get selected value for that. I prefer to use a RadioButtonList to get the selected value.

<asp:RadioButtonList ID="rdl" runat="server">
<asp:ListItem Value="1">A</asp:ListItem>
<asp:ListItem Value="2">B</asp:ListItem>
</asp:RadioButtonList>

then you can get the selected value as:

if(rdl.SelectedIndex!=-1)
somevar=rdl.SelectedValue;

GetSelectedRadioButtonText method():

GetSelectedRadioButtonText which accepts Groupbox as a parameter and returns the selected radio button’s text.

private string GetSelectedRadioButtonText(GroupBox grb) {
           return grb.Controls.OfType<RadioButton>().SingleOrDefault(rad => rad.Checked == true).Text;   
        }
private void buttonOK_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetSelectedRadioButtonText(groupBox1), "Selected Item");  
        }

Example to get Selected Value from Radio button in Web Application

When the Button is clicked, the selected (checked) Text and Value of RadioButtonList will be fetched and will be displayed using JavaScript Alert Message Box.

HTML Markup:

The following HTML Markup consists of an ASP.Net RadioButtonList control, a RequiredFieldValidator, and a Button.

Select Fruit:
<asp:RadioButtonList ID="rblFruits" runat="server">
    <asp:ListItem Text="Apple" Value="1" />
    <asp:ListItem Text="Mango" Value="2" />
    <asp:ListItem Text="Papaya" Value="3" />
    <asp:ListItem Text="Banana" Value="4" />
    <asp:ListItem Text="Orange" Value="5" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="Please select a Fruit.<br />"
    ControlToValidate="rblFruits" runat="server" ForeColor="Red" Display="Dynamic" />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick = "Submit" />

Get Selected Text and Value of RadioButtonList on Button Click in Asp.net

Inside the Click event handler, the Text and Value part of the Selected Item of the RadioButtonList is fetched and displayed using JavaScript Alert Message Box.

protected void Submit(object sender, EventArgs e)
{
    string message = "Selected Text: " + rblFruits.SelectedItem.Text;
    message += "\\nSelected Value: " + rblFruits.SelectedItem.Value;
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}

In this way, we can get the selected value from the radio button in ASP.NET using C#.