5.2. Pushing for Choices with the RadioButton Control

ASP.NET RadioButton controls work as a team; however, only one player can be "on" at a time. Figure 5-2 shows three RadioButton controls acting as a group. All three share the same GroupName value. When a user clicks the Submit button, an event handler subroutine (refer to the "Bingo! And events" sidebar) executes and reports which radio button is checked.

Figure 5-2. You can select only one radio button in a group at a time.

Follow these steps to create a group of RadioButton controls and display which one a user has pushed:

  1. From the Toolbox, add to the ASP.NET page three RadioButton controls, a Button control (Button1) and a Label control (lblText).

  2. Set the RadioButton control's ID values to radTo, radMtl, and radVcr; the Text properties to Toronto, Montreal, and Vancouver; and the GroupName properties to cities.

  3. Double-click the button to create a handler for the Button control's Click event and use the following code inside the Click event handler subroutine:

    If radTo.Checked Then
        lblText.Text = "Choice: " & radTo.Text
    ElseIf radMtl.Checked Then
        lblText.Text = "Choice: " & radMtl.Text
    ElseIf radVcr.Checked Then
        lblText.Text = "Choice: " & radVcr.Text
    Else
        lblText.Text = "No choice made."
    End If

The code tests whether the Toronto radio button's Checked property is True (that is, whether the button is pushed). If so, it assigns ...

Get ASP.NET 3.5 For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.