How to call method that handles the Click event for several

Sep 30 2015 5:03 AM
How to call method that handles the Click event for several buttons ?

Answers (2)

0
Bhushan Bhasme

Bhushan Bhasme

  • 0
  • 812
  • 274.6k
Sep 30 2015 5:26 AM
Hi Chandan,
YOu need To Use EventHandler..
Like
Button b1=new Button();
b1.Text = "Click here to activate";
b1.Location = new Point(20,55);
b1.Size = new Size(150,20);
 
b1.Click+= new EventHandler(GetData); 
btnsample is id of any button andGetData is method which will get call on button click 
 
=====
public void GetData(object sender,EventArgs e) {

//Here You will Get Data Of Button (i.e:properties Of Button)

}
 
Accepted Answer
0
Rashmiranjan Dalabehera

Rashmiranjan Dalabehera

  • 1.1k
  • 663
  • 1.7k
Sep 30 2015 5:29 AM
Hi Chandan,
 
Please clear me what exactly you want?
 
Try this code,if it will help you please accept me.
 
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="handleButtonClick" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="handleButtonClick" />
<asp:Button ID="Button3" runat="server" Text="Button" OnClick="handleButtonClick" />
 
 
protected void handleButtonClick(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.ID=="Button2")
{
Response.Write("<b><i>You have clicked: " + btn.ID+"</i></b>");
}
if (btn.ID == "Button1")
{
Response.Write("<b><i>You have clicked: " + btn.ID + "</i></b>");
}
if (btn.ID == "Button3")
{
Response.Write("<b><i>You have clicked: " + btn.ID + "</i></b>");
}
}