.Net, ASP.NET, Checkbox, Programming, RadioButtonList, VB.NET, Web Development, WebForms

RadioButtonList Binding Overview for ASP.NET WebForms

RadioButtonList in ASP.NET
RadioButtonList in ASP.NET

The RadioButtonList control is situationally useful. Although I don’t find myself using it often, when I do need it, I have to look up the basic functionality and how to apply it. This has led me to write this article as a guide and a code-reference.

Set up the WebForm

For starters, drag and drop a DataSource onto your Web form. In my case I will use a SQLDataSource and I will populate its Select, Update, and Delete commands with stored procedures I have created. My goal is to create a  RadioButtonList  that lists saved default searches for a user and to add a LinkButton control to use as a general delete button.

The following snippet illustrates adding the SQLDataSource:

<asp:SqlDataSource ID="SqlDSMySearchList" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnDb %>"
ProviderName="<%$ ConnectionStrings:ConnDb.ProviderName %>"
CacheDuration="120" SelectCommandType="StoredProcedure"
UpdateCommand="spMyDefaultsU" UpdateCommandType="StoredProcedure" DeleteCommand="spMyDefaultsD" DeleteCommandType="StoredProcedure">
<asp:ControlParameter ControlID="hdnUserId" Name="UserId" PropertyName="Value" />
<asp:ControlParameter ControlID="hdnAppId" Name="AppLookup" PropertyName="Value" />
<asp:Parameter Name="UserId" Type="String" />
<asp:Parameter Name="AppLookup" Type="String" />
<asp:Parameter Name="DefaultId" Type="Int32" />
<asp:Parameter Name="UserId" Type="String" />
<asp:Parameter Name="AppLookup" Type="String" />

Then add the RadioButtonList  and set its DataSource as the Id of the above example SQLDataSource.

<asp:RadioButtonList ID="rbMyDefaultQuery" runat="server" AutoPostBack="True"
DataSourceID="SqlDSMySearchList" DataTextField="QueryName"

Don’t forget to add the LinkButton control that will handle the delete command of the SQLDataSource. In my case I will also add two hidden fields that will store the Id of the User to look up and the Id of the Application.

<asp:LinkButton ID="lbDeleteDefaults" runat="server">Use System-Wide Default</asp:LinkButton>
 <input type="hidden" id="hdnUserId" runat="server" />
 <input type="hidden" id="hdnAppId" runat="server" />

Populating the Supporting Controls

The ControlParameters specified in the SQLDataSource will now pick up the values from the hidden form fields. In order for them to be populated before the SQLDataSource checks them, I populate the hidden form fields in the page’s PreLoad event.

Protected Sub Page_PreLoad(sender As Object, e As System.EventArgs) Handles Me.PreLoad
Dim tmpHdnUserId As HtmlInputHidden = FindControl("hdnUserId")
Dim tmpHdnAppId As HtmlInputHidden = FindControl("hdnAppId")
Dim tmpUserId As String = SecurityUtils.RefineUserId(User.Identity.Name)
tmpHdnUserId.Value = tmpUserId
tmpHdnAppId.Value = Session("xmlPath")
End Sub

Pre-Selecting the RadioButtonList Control from the Database

Now let’s say we want an option in the RadioButtonList to be pre-selected if the user has already saved a choice. In this case, add another SQLDataSource to the WebForm that checks the database for an already saved selection. One then calls the Select option of the SQLDataSource  in the Page’s load event as follows:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
 If Not Page.IsPostBack Then
 Dim dt As DataTable = CType(Me.SqlDSMyDefaultValue.Select(New DataSourceSelectArguments()), DataView).ToTable()
 Dim strTmpQueryName As String = String.Empty
 Dim intTmpQueryId As Int32 = 0
 If dt.Rows.Count > 0 Then
 intTmpQueryId = dt.Rows(0).Item("QueryId")
 strTmpQueryName = dt.Rows(0).Item("QueryName")
 Session("QueryName") = strTmpQueryName
 rbMyDefaultQuery.SelectedValue = intTmpQueryId
 End If
 End If
 End Sub

In the code above the Select command is triggered and is used to populate a DataTable using the following rather neat command:

Dim dt As DataTable = CType(Me.SqlDSMyDefaultValue.Select(New DataSourceSelectArguments()), DataView).ToTable()

Above, the saved value for the RadioButtonList is selected using the SelectedValue property, but there are three ways to set a RadioButtonList value in CodeBehind. These are:

  1. radioButtonId.SelectedValue = “One”
  2. radioButtonId.SelectedIndex = 1
  3. radioButtonId.Items[1].Selected = True


To trigger the SQLDataSource Update command when one of the radio buttons in the RadioButtonList control is clicked can be handled in the SelectedIndexChanged Page Event.

Since the hidden fields are not bound controls, the SQLDataSource Parameters need to be set using the UpdateParameters DefaultValue setting. The final update of course is triggered using the SQLDataSource’s Update() method.

Protected Sub rbMyDefaultQuery_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles rbMyDefaultQuery.SelectedIndexChanged
 Dim tmpHdnUserId As HtmlInputHidden = FindControl("hdnUserId")
 Dim tmpHdnAppId As HtmlInputHidden = FindControl("hdnAppId")
 Dim tmpDefaultItemId As RadioButtonList = FindControl("rbMyDefaultQuery")
 SqlDSMySearchList.UpdateParameters("UserId").DefaultValue = tmpHdnUserId.Value
 SqlDSMySearchList.UpdateParameters("AppLookup").DefaultValue = tmpHdnAppId.Value
 SqlDSMySearchList.UpdateParameters("DefaultId").DefaultValue = rbMyDefaultQuery.SelectedValue
 End Sub


The Delete() method of the SQLDataSource control can be toggled from the LinkButton Control’s click event. As with the Update() method described previously, the  parameter information for the deletion must be manually set using the DeleteParameters DefaultValue setting.

Once the Delete() method is called, the database transaction to remove the necessary records is initiated and the RadioButtonList needs to reflect this. To clear the selection from a RadioButtonList control the SelectedIndex is set to -1 .

Protected Sub lbDeleteDefaults_Click(sender As Object, e As System.EventArgs) Handles lbDeleteDefaults.Click
 Dim tmpHdnUserId As HtmlInputHidden = FindControl("hdnUserId")
 Dim tmpHdnAppId As HtmlInputHidden = FindControl("hdnAppId")
 SqlDSMySearchList.DeleteParameters("UserId").DefaultValue = tmpHdnUserId.Value
 SqlDSMySearchList.DeleteParameters("AppLookup").DefaultValue = tmpHdnAppId.Value
 rbMyDefaultQuery.SelectedIndex = -1
 End Sub

2 thoughts on “RadioButtonList Binding Overview for ASP.NET WebForms”

    1. Hi, I’m glad you found the article useful! Also thanks for pointing me to the mindstick.com article that also reviews the functionality of the ASP.NET RadioButtonList control, it was interesting to read.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s