Export (0) Print
Expand All

RadioButton Web Server Control

Creates an individual radio button on the page. You can group multiple radio buttons together to provide a mutually exclusive set of choices.

<asp:RadioButton id="RadioButton1" 
     AutoPostBack="True|False"
     Checked="True|False"
     GroupName="GroupName"
     Text="label"
     TextAlign="Right|Left"
     OnCheckedChanged="OnCheckedChangedMethod"
     runat="server"/>

Remarks

The RadioButton server control creates a radio button on the Web Forms page. Specify the text to display in the control by setting Text property. The text can appear either on the left or the right of the radio button. Set TextAlign property to control the side that the text appears on. You can group multiple radio buttons together if you specify the same GroupName for each RadioButton control. Grouping radio buttons together will only allow a mutually exclusive selection from the group.

Note   You can also use the RadioButtonList control. The RadioButtonList control is easier for creating a set of radio buttons using data binding, while the individual RadioButton control gives you greater control over layout.

To determine whether the RadioButton control is selected, test the Checked property.

CAUTION   Text is not HTML encoded before it is displayed in the RadioButton control. This makes it possible to embed script within HTML tags in the text. If the values for the control come from user input, be sure to validate the values to help prevent security vulnerabilities.

For detailed information on the RadioButton Web server control's properties and events, see the RadioButton Class documentation.

Example

The following example demonstrates how to use a RadioButton control to provide a set of mutually exclusive options to the user.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub SubmitBtn_Click(Sender As Object, e As EventArgs)
         If Radio1.Checked Then
            Label1.Text = "You selected " & Radio1.Text
         ElseIf Radio2.Checked Then
            Label1.Text = "You selected " & Radio2.Text
         ElseIf Radio3.Checked Then
            Label1.Text = "You selected " & Radio3.Text
         End If
      End Sub
   </script>
</head>
<body>
   <h3>RadioButton Example</h3>
   <form runat="server">
      <h4>Select the type of installation you want to perform:</h4>
      <asp:RadioButton id=Radio1 
           Text="Typical" 
           Checked="True" 
           GroupName="RadioGroup1" 
           runat="server" /><br>
      This option installs the features most typically used.
      <i>Requires 1.2 MB disk space.</i><p>
      <asp:RadioButton id=Radio2 
           Text="Compact" 
           GroupName="RadioGroup1" 
           runat="server"/><br>
      This option installs the minimum files required to run 
      the product.  <i>Requires 350 KB disk space.</i><p>
      <asp:RadioButton id=Radio3 
           Text="Full" 
           GroupName="RadioGroup1"  
           runat="server" /><br>
      This option installs all features for the product.  
      <i>Requires 4.3 MB disk space.</i><p>
      <asp:Button text="Submit" 
           OnClick="SubmitBtn_Click" 
           runat=server/>
      <asp:Label id=Label1 
           Font-Bold="true" 
           runat="server" />
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      void SubmitBtn_Click(Object Sender, EventArgs e) 
      {
         if (Radio1.Checked) 
         {
            Label1.Text = "You selected " + Radio1.Text;
         }
         else if (Radio2.Checked) 
         {
            Label1.Text = "You selected " + Radio2.Text;
         }
         else if (Radio3.Checked) 
         {
            Label1.Text = "You selected " + Radio3.Text;
         }
      }
   </script>
</head>
<body>
   <form runat="server">
      <h3>RadioButton Example</h3>
      <h4>Select the type of installation you want to perform:</h4>
      <asp:RadioButton id="Radio1" 
           Text="Typical" 
           Checked="True" 
           GroupName="RadioGroup1" 
           runat="server" /><br>
      This option installs the features most typically used.  
      <i>Requires 1.2 MB disk space.</i><p>
      <asp:RadioButton id="Radio2" 
           Text="Compact" 
           GroupName="RadioGroup1" 
           runat="server"/><br>
      This option installs the minimum files required to run the product.  
      <i>Requires 350 KB disk space.</i><p>
      <asp:RadioButton id="Radio3"  
           Text="Full" 
           GroupName="RadioGroup1" 
           runat="server"/><br>
      This option installs all features for the product.  
      <i>Requires 4.3 MB disk space.</i><p>
      <asp:Button id="Button1" 
           Text="Submit" 
           OnClick="SubmitBtn_Click" 
           runat=server/>
      <asp:Label id="Label1" 
           Font-Bold="true" 
           runat="server" />
   </form>
</body>
</html>

See Also

Web Server Controls | RadioButton Class

Show:
© 2014 Microsoft