This documentation is archived and is not being maintained.

Setting and Getting the Selection in a RadioButton Web Server Control

Visual Studio .NET 2003

You can set a selected radio button at design time or at run time in code. If the radio button is in a group, setting it clears any other selection in the group.

Note   If you are working with a RadioButtonList control, the procedure for getting and setting the value of a button is different. For details, see Determining the Selection in a List Web Server Control and Setting the Selection in a List Web Server Control.

To set the selected RadioButton control

  • Set the control's Checked property to true. If you select more than one RadioButton control in a group, the browser determines which button is rendered as selected.

    If you set the property to false, it clears the selection but does not select another radio button. Therefore, you can clear all selections by setting the Checked property of all radio buttons in a group to false.

Determining which RadioButton control has been selected is a matter of testing the Checked property.

To determine which RadioButton control in a group is selected

  • Test the control's Checked property.
    Note   Testing the value of a radio button does not tell you if the user changed the value of the control, only if it is selected. To check for a change in the control, write an event handler for the control's CheckedChanged event. For details, see Responding to a User Selection in a RadioButton Group.

    To determine which is checked in a group, you have to test each control individually, as in the following example:

    ' Visual Basic
    Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim msg As String = "You selected "
       If RadioButton1.Checked = True Then
          msg = msg & RadioButton1.Text
       ElseIf Radiobutton2.Checked = True Then
          msg = msg & RadioButton2.Text
       ElseIf Radiobutton3.Checked = True Then
          msg = msg & RadioButton3.Text
       End If
       Label1.text = msg
    End Sub
    // C#
    public void Button1_Click (object sender, System.EventArgs e)
       if (RadioButton1.Checked) {
          Label1.Text = "You selected " + RadioButton1.Text;
       else if (RadioButton2.Checked) {
          Label1.Text = "You selected " + RadioButton2.Text;
       else if (RadioButton3.Checked) {
          Label1.Text = "You selected " + RadioButton3.Text;

See Also

Adding Individual RadioButton Controls to a Web Forms Page | Responding to a User Selection in a RadioButton Group | RadioButton Class | RadioButtonList Class