Omer
Omer

Reputation: 317

Activating an event function from the code behind

I have this function, that is activated on a button click:

public void button1_Click(object sender, EventArgs e)

I want to use this function, also from another function in the code behind.

For example, like this:

public void button2_Click(object sender, EventArgs e){
button1_Click();
}

What do you send to button1_Click from button2_Click as my sender and e? or is there another option to use it without sending anything somehow?

Upvotes: 0

Views: 42

Answers (1)

Hanlet Escaño
Hanlet Escaño

Reputation: 17370

What about this:

protected void Button1_Click(object sender, EventArgs e)
{
    doSomething();
}
protected void Button2_Click(object sender, EventArgs e)
{
    doSomething();
}

void doSomething()
{
   ...
}

If you "must" call it from Button2_Click, and you don't actually use the sender and EventArgs, then you can do this:

Button2_Click(null, null);

Upvotes: 1

Related Questions