共用方式為


How to: Respond to a User Selection in a RadioButton Web Server Control Group

When the user selects a RadioButton control, the control raises an event that you can respond to.

Note

The RadioButtonList control raises events differently than individual RadioButton controls. For details, see Responding to Changes in a List Web Server Control.

You might not need to respond directly to the selection event of a RadioButton control at all. You respond to the event only if it is important to know when the user has made a change to the selection in a radio button group.

If you are only interested in knowing which radio button is selected, and not whether selection has changed, you can simply test radio button selection after the form has been posted to the server. For details about determining which RadioButton control is selected, see How to: Set and Get the Selection in a RadioButton Web Server Control.

Because each RadioButton server control is a separate control, each can raise an event individually; the radio button group does not raise an event as a whole.

To respond to a selection in a RadioButton control

  • Create an event handler for the control's CheckedChanged event.

    By default, the CheckedChanged event does not immediately cause the Web Forms page to be posted to the server. Instead, the event is raised in server code the next time the form is posted, as when a Button Web server control is clicked. To have the CheckedChanged event cause an immediate posting, set the RadioButton control's AutoPostBack property to true.

    Note

    The ability of a RadioButton control to post to the server when it is checked requires that the browser support ECMAScript (JScript, JavaScript) and that scripting be enabled on the user's browser.

    The following code example shows how you can respond when a user selects a RadioButton control.

    Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) _
            Handles RadioButton1.CheckedChanged
       Label1.Text = "You selected Radio Button " & RadioButton1.Text
    End Sub
    
    public void RadioButton1_CheckedChanged (object sender, 
        System.EventArgs e)
    {
       Label1.Text = "You selected Radio Button " + RadioButton1.Text;
    }
    

See Also

Reference

RadioButton and RadioButtonList Web Server Controls Overview