How to Program Radio Buttons in Vb.Net

Written by brian kemple
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Program Radio Buttons in Vb.Net
ASP.Net's RadioButton controls allow programmers to handle data entry and retrieval easily. (Stockbyte/Stockbyte/Getty Images)

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:
Easy

Other People Are Reading

Instructions

  1. 1

    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.

  2. 2

    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.

  3. 3

    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.

  4. 4

    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

    End If

    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.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.