Home > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlInputRadioButton
HtmlInputRadioButton Class Input Radio Element
Creates a selectable radio button control.
Declarative Syntax
<input type=radio id="accessID" runat="server"
checked
name = "radiobuttongroup"
value = "radioButtonValue"
>
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.
The 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 find the selected radiobutton from a group of HtmlRadioButton controls. In this case, the checked property is simply evaluated and a message of the result is rendered via a <p runat=server
> 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">
<p><b>The first president of the United States was:</b></p>
<table width=300 cellpadding=5 bgcolor="beige" style="border:1px inset">
<tr><td><label><input type=radio id="Radio1" name="choices"
runat="server">Abraham Lincoln</label></td></tr>
<tr><td><label><input type=radio id="Radio2" name="choices"
runat="server">Christopher Columbus</label></td></tr>
<tr><td><label><input type=radio id="Radio3" name="choices"
runat="server">George Washington</label></td></tr>
<tr><td><label><input type=radio id="Radio4" name="choices"
runat="server">Robert Plant</label></td></tr>
</table>
<p><button onServerClick="chkAnswer" runat="server">Submit</button>
<p id="msg" runat="server" />
</form>
- Create event handling code to process the information gathered from the radio button group.
<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 src, EventArgs e ) {
string txt = "";
if ( Radio1.Checked )
txt += answers [ 0 ];
else if ( Radio2.Checked )
txt += answers [ 1 ];
else if ( Radio3.Checked )
txt += answers [ 2 ];
else if ( Radio4.Checked )
txt += answers [ 3 ];
msg.InnerHtml = txt;
}
</script>
HtmlInputRadioButton Class Input Radio Element Web Forms Events and Handlers