C# Bubbling Events

I need to bubble an event up from one control to another or a top level page in my ASP.NET app … hmmm … how do I do that again.

Way 1: Standard Event

public class MyClass
{
     public event EventHandler MyEvent;
}

Way 2: Custom EventArgs

public class MyClass
{
     public delegate void MyCustomEventHandler(object sender, MyCustomEventArgs e);

     public event MyCustomEventHandler MyEvent;

     protected virtual void OnMyCustomEventHandler(object sender, MyCustomEventArgs e)
     {
          MyCustomEventHandler handler = MyEvent;
          if (handler != null) { handler (this, e); }
     }
}

// Custom Event Args
public class MyCustomEventArgs : System.EventArgs
{
     public string item1 = "";
     public string item2 = "";
     public MyCustomEventArgs(string i1, string i2)
     {
          item1 = i1;
          item2 = i2;
     }
}
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: