Explicitly calling an event handler

  • Thread starter Thread starter Harold Crump
  • Start date Start date
H

Harold Crump

Greetings,

Is there a way using the .net framework to explicitly call a particular
event handler method in an object, say the Click event of a control?

The scenario is that I have two controls - an ImageButton and a
LinkButton.
When the LinkButton is clicked, I want to call the Click event handler
in the ImageButton.

Something like :
void LinkButton_Click(Object sender, EventArgs e)
{
ImageButton1.Click(sender, e);
}

This code doesn't compile but is there a way to do something like this?

More info - there will be several such "sets" of ImageButton/LinkButton
combos on the page.
Each LinkButton will know about the ImageButton it is "linked" to, but
they won't be aware of the other sets on the page.

Thanks.
 
The event handler for a button is called by asp.net when a post-back to the
server is performed. Therefore it does not make sense to try to fire an
event for another control when handling because all post-backs are initiated
by the client. If all you want to do is execute the same code in response to
the user clicking the image button or the link button, why don't you create a
new common method that you can call from both event handlers?

void LinkButton_Click(Object sender, EventArgs e)
{
DoSomeWork();
}

void ImageButton_Click(Object sender, EventArgs e)
{
DoSomeWork();
}

HTH, Jakob.
 
Harold Crump said:
Greetings,

Is there a way using the .net framework to explicitly call a particular
event handler method in an object, say the Click event of a control?

The scenario is that I have two controls - an ImageButton and a
LinkButton.
When the LinkButton is clicked, I want to call the Click event handler
in the ImageButton.

Something like :
void LinkButton_Click(Object sender, EventArgs e)
{
ImageButton1.Click(sender, e);
}

This code doesn't compile but is there a way to do something like this?

More info - there will be several such "sets" of ImageButton/LinkButton
combos on the page.
Each LinkButton will know about the ImageButton it is "linked" to, but
they won't be aware of the other sets on the page.

Thanks.

There are two issues with what you wish to accomplish. First, you cannot
raise an event outside the containing class - the Click event of the
ImageButton can only be raised inside the IimageButton code. Second, the
ImageButton has a different event handler parameter signature than the
LinkButton. The ImageButton requires an ImageClickEventArgs value, not an
EventArgs value, so you cannot just send the LinkButton 'e' variable value
to the event handler routine for the ImageButton.

If you can turn your requirement around a little bit, you could put the code
that performs the work in the LinkButton event handler, and call the event
handler code from the Image button event handler method:

//----------------------------------------------
private void LinkButton1_Click(object sender, System.EventArgs e)
{
//Perform actions here...
}

private void ImageButton_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
//Note: This does not raise the click event of the LinkButton, it simply
calls the event handler method.
LinkButton1_Click(sender, e);
}
//----------------------------------------------

Alternately, you can create a third method that both of the event handlers
call:

//----------------------------------------------
private void ImageButton1_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
DoSomething(sender, e);
}

private void LinkButton1_Click(object sender, System.EventArgs e)
{
DoSomething(sender, e);
}

private void DoSomething(object sender, System.EventArgs e)
{
// Perform actions here...
}
//----------------------------------------------
 
ImageButton1.Click(sender, e);

Don't try to fire the event, just call the handler:

ImageButton1_Click(null,null);
 
Back
Top