peplamb peplamb - 1 year ago 72
C# Question

How do i raise an event in a usercontrol and catch it in mainpage?

I have a

, and I need to notify the parent page that a button in the
was clicked. How do I raise an event in the
and catch it on the Main page? I tried using
, and many suggested me to go for events!

Answer Source

Check out Event Bubbling --

Edit to add a quick example: (and additional edit to improve formatting)

User Control

public event EventHandler StatusUpdated;

private void FunctionThatRaisesEvent()
    //Null check makes sure the main page is attached to the event
    if (this.StatusUpdated != null)
       this.StatusUpdated(new object(), new EventArgs());

Main Page/Form

public void MyApp()
     //USERCONTROL = your control with the StatusUpdated event
     this.USERCONTROL.StatusUpdated += new EventHandler(MyEventHandlerFunction_StatusUpdated);

public void MyEventHandlerFunction_StatusUpdated(object sender, EventArgs e)
         //your code here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download