asp.net.ph

Skip Navigation Links

RadioButton Control Syntax

ASP.NET Syntax   ASP.NET Syntax for Web Controls


Creates an individual radio button on the page. You can group multiple radio buttons to present mutually exclusive choices.

Declarative Syntax

NOTE: The content of an <asp:RadioButton> control can be defined within its opening tag. In this case, you can close the start tag with a trailing slash /> instead of using a separate end tag.

For information on the individual members of this class, see RadioButton in the class library.

Remarks

The RadioButton control renders a radio button on a Web Forms page. 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.

Syntax Example

The below code snippet demonstrates how to use a RadioButton control.

Within the <head> of a Web Forms page:

<script language="C#" runat="server">
string [ ] answers = {
   "Abraham Lincoln came a tad bit later.", 
   "Christopher Columbus was an explorer, <br>not a United States President.", 
   "Correct. George Washington was the first President <br>of the United States of America.", 
   "Robert Plant was the lead vocalist of Led Zeppelin, <br>not a United States President."};

void chkAnswer ( Object Sender, EventArgs e ) {
   string msg;
   if ( Radio1.Checked )
      msg = answers [ 0 ];
   else if ( Radio2.Checked )
      msg = answers [ 1 ];
   else if ( Radio3.Checked )
      msg = answers [ 2 ];
   else if ( Radio4.Checked )
      msg = answers [ 3 ];
   Message.InnerHtml = msg;
}
</script>
  C# VB

Within the <body> of a Web Forms page:

<form runat="server">

   <h5>The first president of the United States was:</h5>

   <table>
   <tr><td>
      <asp:RadioButton id="Radio1" Text = "Abraham Lincoln" 
         GroupName = "choices" runat="server" />
<br>
      <asp:RadioButton id="Radio2" Text = "Christopher Columbus" 
         GroupName = "choices" runat="server" />
<br>
      <asp:RadioButton id="Radio3" Text = "George Washington" 
         GroupName = "choices" runat="server" />
<br>
      <asp:RadioButton id="Radio4" Text = "Robert Plant" 
         GroupName = "choices" runat="server" />
<br>
   </td></tr>
   </table>

   <p><asp:button text = "Submit" OnClick = "chkAnswer" runat="server" />

   <p id="Message" runat="server" />

</form>
RadioButton Example
Run Sample | View Source
See Also

RadioButton Class   RadioButton and RadioButtonlist Web Server Controls



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note