Freigeben über


How to: Set and Get the Selection in a RadioButton Web Server Control

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 How to: Determine 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 whether the user changed the value of the control, only whether it is selected. To check for a change in the control, write an event handler for the control's CheckedChanged event. For details, see How to: Respond to a User Selection in a RadioButton Group.

    To determine which button is selected in a group, you have to test each control individually, as in the following code example.

    Protected 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
    
    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

Reference

RadioButton and RadioButtonList Web Server Controls Overview