Working with radio buttons in ASP.Net is usually pretty easy… but what do you do if you need to retrieve your selections from a database and conditionally format them? You have a couple of choices:
- Use a Repeater to build and format all of the RadioButton controls
- Use code to instantiate RadioButton controls
- Use a RadioButtonList control
The Repeater control seems like a logical choice, as it allows you to create your own control template. However, in practice this doesn’t work; the RadioButton controls are not mutually exclusive (i.e. you can select more than one).
Using code in your codebehind does allow for a great deal of control when building and formatting a set of RadioButton controls. Selecting works proper, but because all the controls are built at run time, trying to capture the OnClick event is problematic.
The last choice is to use a RadioButtonList control This control takes all the radio button labels and values from the databound datasource; events are channeled through a single event handler for the RadioButtonList. So what’s the problem here? You can’t databind any of the attributes to other fields in the datasource.
This is where the RadioButtonList’s DataBound event comes in handy. The DataBound event is fired after a datasource is bound to a control; this is the perfect place to do some reformatting.
Once the event is fired, you have two objects you are interested in; the RadioButtonList and the datasource. You get a reference to the original datasource from RadioButtonList.DataSourceObject. Once you have the datasource, you can use it to produce a dataview, which is what we will use to customize our control
Protected Sub rblLocations_DataBound( _ ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles rblLocations.DataBound Dim ds As New SqlDataSource Dim dv As New Data.DataView Dim rbl As RadioButtonList ' sender is the object sending... in this case a RadioButtonList rbl = CType(sender, RadioButtonList) ' get the datasource from the control ds = rbl.DataSourceObject ' let us take a look dv = ds.Select(DataSourceSelectArguments.Empty)
The dataview contains only the data bound to the control, so rows are filtered, ordered, etc. as per the original datasource. This makes it easy to correlate rows in the dataview to collection items in the databound control.
One important thing to note: a dataview does not allow you to refer to a datacolumn by name; the datacolumns are in the same order as the original datasource query. Just refer to the columns by number, keeping in mind that datarows and datacolumns use zero-based indices.
Dim i as Integer = 0 For Each li As ListItem In rbl.Items ' change label text to include additional information li.Text = dv.Item(i).Item(0) & "<em> (" & dv.Item(i).Item(2) _ & " of " & dv.Item(i).Item(1) & " seats remaining)</em>" ' conditionally enable/disable the control li.Enabled = (dv.Item(i).Item(2) > 0) i += 1 Next End Sub
There you have it… you can databind your RadioButtonList to a datasource and still have control over the formatting without resorting to black magic!






