Controls You Can Use on Web Forms ASP.NET Standard Controls RadioButton and RadioButtonList Controls
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 Control and Setting the Selection in a List 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.
<asp:RadioButton id="Radio1" Checked=true Text="Typical" runat="server" />
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.
- 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-handling method 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:
void getSelected ( object src, EventArgs e ) {
String msg = "You selected ";
if ( Radio1.Checked )
msg += Radio1.Text;
else if ( Radio2.Checked )
msg += Radio2.Text;
else if ( Radio3.Checked )
msg += Radio3.Text;
Label1.Text = msg;
}
Sub getSelected ( ByVal src As Object, ByVal e As EventArgs )
Dim msg As String = "You selected "
If Radio1.Checked = True Then
msg = msg & Radio1.Text
ElseIf Radio2.Checked = True Then
msg = msg & Radio2.Text
ElseIf Radio3.Checked = True Then
msg = msg & Radio3.Text
End If
Label1.Text = msg
End Sub |
|
C# |
VB |
Adding Individual RadioButton Controls to a Web Forms Page Responding to a User Selection in a RadioButton Group