Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a Button click event in code behind

Tags:

c#

asp.net

I'm building a button in my code behind file and want to bind the click function to an existing click function of a different button. How do I handle this? I was hoping for something simple like:

Button b = new Button();
b.onClick = otherClickEvent();

but apparently that's not doable. Am I missing something here. I'm aware of OnClientClick, but that only deals with the JS side of things apparently.

like image 706
Adam Avatar asked Nov 28 '22 23:11

Adam


2 Answers

Button b = new Button();
b.Click += new EventHandler(b_Click);

and later on the page

private void b_Click(object sender, EventArgs e)
{
    your code
}
like image 151
Diego Avatar answered Dec 10 '22 05:12

Diego


b.OnClick += otherClickEvent;

To add event handlers in C#, use +=. To remove them, use -=.

like image 21
AJ. Avatar answered Dec 10 '22 05:12

AJ.