The ASP.Net framework provides two powerful options for RadioButtons: the single <asp:RadioButton /> control and the <asp:RadioButtonList> control. Both have the standard appearance of an HTML radio button and both can cause a page postback on selection, capture a user's selection and bind data. The RadioButtonList control is particularly useful in Visual Basic (VB) .NET applications, as it simplifies binding and data submission, and it has built-in mutual exclusivity. However, you can more easily customise individual RadioButtons in appearance and provide better flexibility in applications where the front end spans a large portion of the page.
- Skill level:
Assign each control a unique ID (e.g., <asp:RadioButton ID="rbut1" runat="server" />.) Configuring your RadioButton and RadioButtonList controls correctly is key to using them with more sophisticated VB commands. If a RadioButtonList is being databound, then each list item will be generated dynamically, and you will not need to specify text properties outside the code-behind file. It is also important, depending upon the purpose of the application, to specify a true or false value for the Checked property.
Specify the data source for the RadioButtonList, either in the code-behind file or in the control's properties. Binding data to RadioButton controls works similar to other databinding operations. The list will be dynamically generated from the data source, but you may add static list items by specifying an AppendDataBoundItems property's value as true. The various list items will be mutually exclusive so long as they are within the same RadioButtonList control.
Databind individual RadioButtons in the code-behind file or within code blocks. For instance, taking "rbut1" as the ID of the RadioButton and using a declared and configured data reader, you could set the button's text value like so: rbut1.Text = dataReader.Item("Field"). In order to make individual RadioButtons work together logically, you will need to assign them a GroupName property with an identical value for each button in the group.
Retrieve the checked value by declaring a variable as either an integer or a string, and assign it a yes or no equivalent value based on the result of the RadioButton, e.g.,:
Dim rbutVal As Integer
If rbut1.Checked = True Then
rbutVal = 1
The only operational property of a RadioButton or RadioButtonList transferred in a postback or to the target of a form control's action property is the Checked value. The Checked property only has options of true or false.
Tips and warnings
- Be sure to add a DataValueField property to the RadioButtonList control when dynamically populating its list items; otherwise, it will be difficult to retrieve selected values.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for