Home > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlInputRadioButton
ASP.NET Syntax ASP.NET Syntax for HTML Controls
Creates a selectable radio button control.
Declarative Syntax
<input type = radio id="accessID" runat="server"
checked
name = "radiobuttongroup"
>
For information on the individual members of this class, see HtmlInputRadioButton in the class library.
- This control does not require a closing tag.
The HtmlInputRadioButton control creates a single radio button input field. By default, each individual radio button can be selected.
You can group a set of radio buttons together in cases where only one button from the set of options needs to be selected. This is done by setting a common value for the Name attribute on each radio button in the group. For example, the following snippet creates a group of radio buttons in which only one radio button may be selected.
<input type=radio name = "optgroup1">Red
<input type=radio name = "optgroup1">White
<input type=radio name = "optgroup1">Blue
The selected state must be still be tested on the individual radio buttons, however.
HtmlInputRadioButton enables programming of the HTML <input type=radio
> element. The sample below illustrates use of the HtmlInputRadioButton control.
This example shows a simple way to dynamically gather and display the value selected from a group of HtmlRadioButton controls. In this case, the value is simply rendered via a <span
> generic control.
Note that the HtmlRadioButton control does not have an event that notifies the server when a user has selected a radio button option. You must rely on using one of the button controls such as the HtmlInputButton, HtmlInputImage, or HtmlButton to cause a postback that will allow to program the HtmlRadioButton control itself.
To respond to User Selection in Radio button controls:
- Declare a set of radio button controls, a button control that will cause postback when clicked by the user, and a <
span
> control to render the postback message.
<form runat="server">
<input type=radio id="Radio1" name = "flavor"
value="Strawberry" runat="server" />
Strawberry<br>
<input type=radio id="Radio2" name = "flavor"
value="Vanilla" runat="server" />
Vanilla<br>
<input type=radio id="Radio3" name = "flavor"
value="Chocolate" runat="server" />
Chocolate
<p><input type=button id="Button1" value="Enter"
onServerClick = "Button1_Click" runat="server">
<p><span id="Message" runat="server" />
</form>
- Create event handling code to process the information gathered from the radio button group.
<script language="C#" runat="server">
void Button1_Click ( object Source, EventArgs e ) {
Message.InnerHtml = "Looks like you prefer ";
if ( Radio1.Checked ) Message.InnerHtml += Radio1.Value;
else if ( Radio2.Checked ) Message.InnerHtml += Radio2.Value;
else if ( Radio3.Checked ) Message.InnerHtml += Radio3.Value;
else Message.InnerHtml = "";
}
</script>
HtmlInputRadioButton Class Web Forms Events and Handlers