In this article I will explain with an example, how to populate (bind) RadioButtonList control using SqlDataSource in ASP.Net.
The SqlDataSource control will populate the RadioButtonList control from SQL Server database in ASP.Net.
This article will also explain how to fetch the Selected Item Text and Value of the RadioButonList which is populated using SqlDataSource in ASP.Net.
This article makes use of a table named Fruits whose schema is defined as follows.
The Fruits table has the following records.
Note: You can download the database table SQL by clicking the download link below.
RadioButtonList control and its Properties
The RadioButtonList control makes use of the following properties when it is being populated using the SqlDataSource control.
DataSourceID – The ID of the SqlDataSource control is set as value to this property, this way the RadioButtonList control determines its source of data.
DataTextField – The name of the column to be set as Text in RadioButtonList. These values will be visible to end user.
DataValueField – The name of the column to be set as Value in RadioButtonList. These values will not be visible to end user.
The following HTML Markup consists of an ASP.Net RadioButtonList control, a Button and ASP.Net SqlDataSource control.
The SqlDataSource control is set with the following properties.
1. ConnectionString – Name of the Connection String setting in the Web.Config file.
2. SelectCommand – The Select statement to fetch the records from the Fruits table.
The ID of the SqlDataSource control is set as DataSourceID of the RadioButtonList control.
<asp:RadioButtonList ID="rblFruits" runat="server" DataSourceID="SqlDataSource1"
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>"
SelectCommand="SELECT FruitName, FruitId FROM Fruits">
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="Submit" />
Getting the Selected Text and Value of the ASP.Net RadioButtonList
protected void Submit(object sender, EventArgs e)
string message = "Value: " + rblFruits.SelectedItem.Value;
message += " Text: " + rblFruits.SelectedItem.Text;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
Protected Sub Submit(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)