Recognize Clicks on a Radio Button That Are in a Group and Call a Function
I Have 5 Radio Buttons in My Project That Are in a Group. I Want Only One Function to Be Called When Each Radio Button Is Clicked and to Check Which Radio...
I have 5 radio buttons in my project that are in a group. I want only one function to be called when each radio button is clicked and to check which radio buttons have been selected so that the commands related to Run each one
2 Answers
Try This
<asp:RadioButton ID="rdoButton1" GroupName="Group1" Text="Option1" Value="Option1" runat="server" OnCheckedChanged="Group1_CheckedChanged" AutoPostBack="true" />
<asp:RadioButton ID="rdoButton2" GroupName="Group1" Text="Option2" Value="Option2" runat="server" OnCheckedChanged="Group1_CheckedChanged" AutoPostBack="true" />
Code Behind
protected void Group1_CheckedChanged(Object sender, EventArgs e)
{
if (rdoButton1.Checked) {
//Code for Radio 1
}
if (rdoButton2.Checked) {
//Code for Radio 2
}
}
In Visual Studio you can all your Radio-Buttons "Click" Handler target the same method. To switch between them, you can simply use the Tag property, or cast the object sender param of the click handler to RadioButton and check for the name and call a specific line of code.