Using CreateDelegate with different signatures-Collection of common programming errors
I am writing a custom control that is an extender for an ASP.Net Control. My extender needs to add an EventHandler to the Control to perform some action when an event occurs. In most cases, the control will be a button and the event will be Click. However, the extender will not always be used on a button. The user gives the control ID and EventName at design time.
At runtime, my extender gets a reference to the Control and the Event. Then it adds a handler to that event with the following code:
// – Init
Delegate d = Delegate.CreateDelegate(TheEvent.EventType,this,”ClickHandler”);
TheEvent.AddEventHandler(TheButton, d);
//
void ClickHandler(object sender, EventArgs e)
{
//some logic
}
This works for a button’s Click event and for a drop down’s SelectedItemChanged event. However, it will not work for the Click event of an ImageButton. The signature of the click event for the image button is different. The second parameter is an ImageClickEventArgs instead of an EventArgs.
The ImageClickEventArgs class inherits from EventArgs but I still get the following runtime error.
“Error binding to target method.”
At design time I do not know all the possible controls and events that this extender may be used with. I just need to intercept the event and perform a task. I do not need the EventArgs parameter for my task.
Any suggestions on how to get this working?