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 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
    }
}
0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest