Apr 13 2006

Define a Custom Event inside UserControls

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 08:51

If you are working with either a new custom server control or a normal web user control, this is the proper way of adding an event that can be handled by the parent control of the child control you are develping:

Add the following to the control (Server Control / Web User Control ) being developed:

#region Event Handler 
private static readonly object EventBubbleRaised = new object(); 
public event EventHandler BubbleRaise 
                Events.AddHandler(EventBubbleRaised, value); 
               Events.RemoveHandler(EventBubbleRaised, value); 
protected virtual void OnBubbleClick(EventArgs e) 
              EventHandler BubbleRaised = (EventHandler)Events[EventBubbleRaised]; 
              if (BubbleRaised != null) 
                      BubbleRaised(this, e); 

In the parent page / control, you add this in the Init method:

BubbleControl.BubbleRaise += new EventHandler(ControlName_BubbleRaise);

Then you define you our handler:

private void ControlName_BubbleRaise(object sender, EventArgs e) 


That was a simple example, but at least you know now the steps.


Tags: ,

Comments are closed