Take the 2-minute tour ×

I have user control that inherits a base control class and these user controls are loaded using the LoadControl method, I can't seem to figure out how to raise events from user controls to the page that are dynamically loaded this way.


Here is the delegate and event in the base user control class.

public delegate void SomeChangeEventHandler(object sender
, SomeChangeEventArgs e);

public event SomeChangeEventHandler SomeChangeEvent; 

public virtual void OnSomeChanged(SomeChangeEventArgs e)
    if (SomeChangeEvent != null)
       SomeChangeEvent(this, e);
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You can add event handlers manually like this:

MyUserControl myControl1 = (MyUserControl)LoadControl("ThisControl.ascx.cs");
myControl1.DataBinding += new System.EventHandler(this.MyControl_DataBinding);
share|improve this answer
I guess I don't understand what yor saying, I can add the event handler at the time I load the control? –  OutOFTouch Feb 23 '10 at 18:44
I modified my answer to include an answer to your comment. –  Gabriel McAdams Feb 23 '10 at 18:55
Please remember to accept this answer if you found it useful. –  Gabriel McAdams Feb 23 '10 at 19:31
This is close but my event is in a control base class and the page can't see the method becuase the control is just a generic UserControl to the page therefore it can't see the custom event. Do I need to cast this to correct type first? –  OutOFTouch Feb 23 '10 at 20:51
Yes. I modified my answer (slightly) to show a type other than UserControl. –  Gabriel McAdams Feb 23 '10 at 21:14
show 7 more comments

Your Answer


