Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all.

I have about 700 buttons in my aspx. All buttons will do the same event so I want to declare only one buttonclick event so that all buttons will fire to that only event. How can I do that?

Tq!
Posted

You can declare as many buttons you want and assign 1 onclick function :)


Tested for 4 buttons
XML
<asp:Button ID="btn1" runat="server" Text="btn1" onclick="btn1_Click" />
    <asp:Button ID="Button1" runat="server" Text="btn1" onclick="btn1_Click" />
     <asp:Button ID="Button2" runat="server" Text="btn1" onclick="btn1_Click" />
      <asp:Button ID="Button3" runat="server" Text="btn1" onclick="btn1_Click" />
 
Share this answer
 
v2
Comments
snamyna 26-Sep-12 4:07am    
Then, how can I determine which button actually cause the event to be fired? or in other words, how can I declare the button ID?
Pro Idiot 26-Sep-12 6:23am    
In the btn1_Click function


protected void btn1_Click(object sender, EventArgs e)
{
Button b1 = (Button)sender;
string test = b1.ID;
}

here you get your button id , do appropriate task
snamyna 12-Oct-12 2:44am    
Thanks! :)
Define a single button Click event handler and attach that handler to all the buttons.
C#
btn1.Click += new EventHandler(this.GenericBtn_Click);
btn2.Click += new EventHandler(this.GenericBtn_Click);
btn3.Click += new EventHandler(this.GenericBtn_Click);
btn4.Click += new EventHandler(this.GenericBtn_Click);
.
.
.
btnn.Click += new EventHandler(this.GenericBtn_Click);

void GenericBtn_Click(Object sender, EventArgs e)
{
  // all button clicks land here!
}


Refer: MSDN: Button.Click Event[^]
 
Share this answer
 
Define a single button Click event handler and attach that handler to all the buttons.
C#
btn1.Click += new EventHandler(this.GenericBtn_Click);
btn2.Click += new EventHandler(this.GenericBtn_Click);
btn3.Click += new EventHandler(this.GenericBtn_Click);
btn4.Click += new EventHandler(this.GenericBtn_Click);
.
.
.
btnn.Click += new EventHandler(this.GenericBtn_Click);

void GenericBtn_Click(Object sender, EventArgs e)
{
  // all button clicks land here!
}


Refer: MSDN: Button.Click Event[^]
 
Share this answer
 

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