In this article I will explain a tutorial with example, how to use the OnSelectedIndexChanged event of RadioButtonList control in ASP.Net using C# and VB.Net.
This article will also explain how to get the Selected Text and Value inside the OnSelectedIndexChanged event of RadioButtonList control in ASP.Net using C# and VB.Net.
 
 
Database
This article makes use of a table named Fruits whose schema is defined as follows.
ASP.Net RadioButtonList OnSelectedIndexChanged event Tutorial with example in C# and VB.Net
 
The Fruits table has the following records.
ASP.Net RadioButtonList OnSelectedIndexChanged event Tutorial with example in C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

HTML Markup
The HTML Markup consists of an ASP.Net RadioButtonList. The RadioButtonList has been assigned an OnSelectedIndexChanged event handler and the AutoPostBack property is set to True.
The AutoPostBack property when set to True, it will raise PostBack when an item is selected or changed in the RadioButtonList and the OnSelectedIndexChanged event will be raised.
If the AutoPostBack property is not set to True then even though the OnSelectedIndexChanged event handler is assigned, still the OnSelectedIndexChanged event will not be raised.
<asp:RadioButtonList ID="rblFruits" runat="server"
    OnSelectedIndexChanged = "OnRadio_Changed" AutoPostBack = "true">
</asp:RadioButtonList>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Populating RadioButtonList control from database in ASP.Net
Inside the Page Load event of the page, the RadioButtonList is populated with the records of the Fruits Table.
The FruitId and the FruitName column values are fetched from the database using SqlDataReader and are assigned to the DataTextField and DataValueField properties of the RadioButtonList control.
DataTextField – The values of the column set as DataTextField are visible to the user.
DataValueField – The values of the column set as DataValueField are not visible to the user. Generally ID or Primary Key columns are set as values in order to uniquely identify a RadioButtonList Item.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query = "SELECT FruitName, FruitId FROM Fruits";
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                rblFruits.DataSource = cmd.ExecuteReader();
                rblFruits.DataTextField = "FruitName";
                rblFruits.DataValueField = "FruitId";
                rblFruits.DataBind();
                con.Close();
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("Constring").ConnectionString
        Using con As New SqlConnection(constr)
            Dim query As String = "SELECT FruitName, FruitId FROM Fruits"
            Using cmd As New SqlCommand(query)
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                con.Open()
                rblFruits.DataSource = cmd.ExecuteReader()
                rblFruits.DataTextField = "FruitName"
                rblFruits.DataValueField = "FruitId"
                rblFruits.DataBind()
                con.Close()
            End Using
        End Using
    End If
End Sub
 
 
The OnSelectedIndexChanged event handler of the ASP.Net RadioButtonList
When an item is selected in the RadioButtonList, the OnSelectedIndexChanged event handler is raised.
The Value and the Text part of the Selected Item of the ASP.Net RadioButtonList is fetched and displayed using JavaScript Alert message box.
C#
protected void OnRadio_Changed(object sender, EventArgs e)
{
    string message = "Value: " + rblFruits.SelectedItem.Value;
    message += " Text: " + rblFruits.SelectedItem.Text;
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
 
VB.Net
Protected Sub OnRadio_Changed(sender As Object, e As EventArgs)
    Dim message As String = "Value: " & rblFruits.SelectedItem.Value
    message &= " Text: " & rblFruits.SelectedItem.Text
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
 
 
Screenshot
ASP.Net RadioButtonList OnSelectedIndexChanged event Tutorial with example in C# and VB.Net
 
 
Demo
 
 
Downloads