ASP.Net - RadioButtonList

RadioButtonList

The radiobuttonlist control is used to setup a group of radio controls - which you only want one to be selected.

Here is a sample

1
2
3
4
5
<asp:RadioButtonList ID="rlBuildingUse" runat="server">
<asp:ListItem Value="R">Residential</asp:ListItem>
<asp:ListItem Value="M">Multi-Family</asp:ListItem>
<asp:ListItem Value="C">Commercial</asp:ListItem>
</asp:RadioButtonList>

Here is how to programatically set which one is selected

1
2
rlBuildingUse.SelectedIndex = rlBuildingUse.Items.IndexOf( _
rlBuildingUse.Items.FindByValue("C"))

Here is how to bind a radio button list control to an object datasource

1
2
3
4
5
6
7
8
9
<asp:RadioButtonList 
ID="RadioButtonList1"
runat="server"
RepeatDirection="Horizontal"
selectedvalue='<%# bind("LRC") %>'>
<asp:ListItem>L</asp:ListItem>
<asp:ListItem>R</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:RadioButtonList>

This works if the table will always have a value. but if this is an optional value, then you’ll need to be a bit more creative.

Formview

where the value in a radio button list is optional:

1
2
3
4
5
6
7
8
<asp:RadioButtonList 
ID="RadioButtonList1"
runat="server"
RepeatDirection="Horizontal">
<asp:ListItem>L</asp:ListItem>
<asp:ListItem>R</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:RadioButtonList>

Add some logic to the prerender event of the formview control.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Protected Sub fv_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.PreRender
If fv.CurrentMode = FormViewMode.Edit Then
'
' drop down controls are not bound to the form, so we must
' do this manually.
'
Dim obj As bo_acad_LateralLine _
= CType(fv.DataItem, bo_acad_LateralLine)

If IsNothing(obj) Then
' skip - because there is not object
Else

' left right center
If Not IsNothing(CType(fv.FindControl("rblLRC"), RadioButtonList _
).Items.FindByValue(obj.LRC)) Then

CType(fv.FindControl("rblLRC"), DropDownList).SelectedValue _
= obj.LRC
End If
End If
End If
End Sub

And some logic to the ItemUpdating event

1
2
3
4
5
Protected Sub fv_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles fv.ItemUpdating
'
' data column name control name
e.NewValues("LRC") = CType(fv.FindControl("rblLRC"), RadioButtonList).SelectedValue
End Sub

ASP.net 1.1 note

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Dim str As String _
= vbCrLf & "<script> " & vbCrLf _
& " function LookupID() " & vbCrLf _
& " { var assetType;" & vbCrLf _
& " if (document.forms[0].all('" & AssetTypeSelectorRadioButtonList.ClientID & "_0').checked) " & vbCrLf _
& " assetType='Manhole'; " & vbCrLf _
& " if (document.forms[0].all('" & AssetTypeSelectorRadioButtonList.ClientID & "_1').checked) " & vbCrLf _
& " assetType='ServiceLine'; " & vbCrLf _
& " if (document.forms[0].all('" & AssetTypeSelectorRadioButtonList.ClientID & "_2').checked) " & vbCrLf _
& " assetType='SewerLine'; " & vbCrLf _
& " var href='Lookup_All_modal.aspx' " & vbCrLf _
& " + '?AssetCode=' + document.forms[0].all('" & AssetIDTextBox.ClientID & "').value " & vbCrLf _
& " + '&AssetType;=' + assetType" & vbCrLf _
& " ; " & vbCrLf _
& " var args=''; " & vbCrLf _
& " var X = window.showModalDialog(href, args, 'dialogHeight: 550px; dialogWidth:650px; resizable:yes; scroll:yes; status:yes; '); " & vbCrLf _
& " if (X ) " & vbCrLf _
& " { args = X.split('|'); " & vbCrLf _
& " document.forms[0].all('" & AssetIDTextBox.ClientID & "').value=args[0]; " & vbCrLf _
& " } " _
& " } " _
& " </script> "

' you want to do this
' if (document.forms[0].all('" & AssetTypeSelectorRadioButtonList.ClientID & "').options[1].selected) " & vbCrLf
' but what's rendered is individual radio controls.
' so then you want to do this
' if (document.forms[0].all('" & AssetTypeSelectorRadioButtonList.items(1).ClientID & "').selected) " & vbCrLf
' but an item doesn't have a client id
Page.RegisterStartupScript("LookupID", str)