tags:

views:

36

answers:

3

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.

+3  A: 

b.OnClick += otherClickEvent;

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

AJ
+3  A: 
Button b = new Button();
b.Click += otherClickEvent;

The event is Click and expects an EventHandler which is any method with a signature of:

void Method(object sender, EventArgs e);

That is why you add it using just the name of the method, if you add parentheses you are calling that method.

Equiso
+3  A: 

AJ is almost correct here. You can use Method Group Conversion syntax for this and write it like this:

b.OnClick += otherClickEvent;
Øyvind Bråthen
Oops! Right you are. Editing my answer.
AJ