Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have four radio buttons and four method.when I click a radio button appropriate method should be called and display result in label in ASP.NET(asp.net with c#.net).

Thanks in Advance
Posted
Comments
Bandi Ramesh 16-Feb-13 5:36am    
radio buttons or radiobuttonlist?
Rekhash 16-Feb-13 5:44am    
radiobutton @bandi.ramesh
Bandi Ramesh 16-Feb-13 5:46am    
can you post your code?

1 solution

1. Use OnCheckedChanged event (the name of the function to be executed when the Checked property has changed) of Radio Button.

2. Call the same function on that event for all Radio Buttons.

3. In that event, check which radio button is clicked.

4. Show in label about that Radio Button.

Refer - How to use OnCheckedChanged event in RadioButton[^], which gives similar type of requirement.

So, in your aspx, Radio Buttons will look like below.
XML
<asp:RadioButton
             ID="RadioButton1"
             runat="server"
             Text="RadioButton1"
             OnCheckedChanged="RadioButton_CheckedChanged"
             AutoPostBack="true">
</asp:RadioButton>
<asp:RadioButton
             ID="RadioButton2"
             runat="server"
             Text="RadioButton2"
             OnCheckedChanged="RadioButton_CheckedChanged"
             AutoPostBack="true">
</asp:RadioButton>
<asp:RadioButton
             ID="RadioButton3"
             runat="server"
             Text="RadioButton3"
             OnCheckedChanged="RadioButton_CheckedChanged"
             AutoPostBack="true">
</asp:RadioButton>
<asp:RadioButton
             ID="RadioButton4"
             runat="server"
             Text="RadioButton4"
             OnCheckedChanged="RadioButton_CheckedChanged"
             AutoPostBack="true">
</asp:RadioButton>


So, as you can see the function "RadioButton_CheckedChanged" is called for all Radio Buttons' OnCheckedChanged Event.

Now in cs page, do like below.
C#
protected void RadioButton_CheckedChanged(object sender, System.EventArgs e)
{
      if (RadioButton1.Checked)
      {
            // Call function you want to call for "RadioButton1".
            Label1.Text = "You choose: " + RadioButton1.Text;
      }

      if (RadioButton2.Checked)
      {
            // Call function you want to call for "RadioButton2".
            Label1.Text = "You choose: " + RadioButton2.Text;
      }
      
      if (RadioButton3.Checked)
      {
            // Call function you want to call for "RadioButton3".
            Label1.Text = "You choose: " + RadioButton3.Text;
      }
      
      if (RadioButton4.Checked)
      {
            // Call function you want to call for "RadioButton4".
            Label1.Text = "You choose: " + RadioButton4.Text;
      }
}
 
Share this answer
 
v9
Comments
Rekhash 17-Feb-13 23:31pm    
four method.when I click a radio button appropriate method should be called and display result in label @Tadit Dash
Yes, exactly...
As you can see I have commented statements in each if block saying

"// Call function you want to call for "RadioButton1"."

This is where you need to call your function for "RadioButton1".

So, you can call all the functions one by one in each if block and do what you want to do.

Got it?
Please reply.
Rekhash 18-Feb-13 2:47am    
Ok i got it.Thanks @Tadit Dash
Hi Rekhash,

Anytime. My pleasure.

Please accept this answer, if it has helped you in any way.
This will help others to find the answer in one go and you will also be awarded with some points for this action...

Thanks,
Tadit
Hi Rekhash,

You are toggling between accepting and undoing accepting of this answer.

Please make sure you accept this answer and not doing undo again.
Please accept.

Thanks,
Tadit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900