Radio Buttons are used when we need to make multiple sets of choices available, but we want the user to select only one of them. If they click on a second selection after making a first, the first selection is removed and replaced by the second.
<asp:RadioButton ID="RadioButton1" runat="server" Text="Yes" GroupName="a" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="No" GroupName="a" />
Group name of both RadioButton should be same so that user can select ony one
Main event of RadioButton is CheckedChanged event.
<asp:RadioButton ID="RadioButton1" runat="server" Text="Yes" GroupName="a"
AutoPostBack="True" oncheckedchanged="RadioButton1_CheckedChanged" />
<br />
<asp:RadioButton ID="RadioButton2" runat="server" Text="No" GroupName="a"
AutoPostBack="True" oncheckedchanged="RadioButton2_CheckedChanged" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Label ID="Label1" runat="server"></asp:Label>
When you select the given option then CheckedChanged event will fire if AutoPostBack="True".
Function to handle CheckChanged Event.
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
Label1.Text = "You selected " + RadioButton1.Text;
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked)
{
Label1.Text = "You selected " + RadioButton2.Text;
}
}
When you select the given option then CheckedChanged event will fire and it will show the selected value in Label.
We can change RadioButton appearance
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True"
Font-Bold="True" Font-Italic="True" Font-Names="Arial" ForeColor="Red"
Text="Yes" />
<asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True"
Font-Bold="True" Font-Italic="True" Font-Names="Arial" ForeColor="Red"
Text="No" />
Leave Comment