Raising events in a Page and handling it in a WebControl - Raj Kaimal

Raising events in a Page and handling it in a WebControl

Target Audience: ASP.net webcontrol developers.


This article describes a way for a group of custom webcontrols to subscribe to a custom page event and take action based on the event.

Consider a complicated data entry page with lots of fields (Label + Textbox). To help the User understand the fields, we add hyperlinks next to the label controls so that they can get more information about the entry field by clicking on the hyperlink. 
                         ------------------
[Label1] [HyperLink1]   | [TextBox1]       |
                         ------------------

                         ------------------
[Label2] [HyperLink2]   | [TextBox2]       |
                         ------------------

We would like the User to have the option to turn off all hyperlinks once they get used to the web interface (as part of their profile). One way is to hide all help hyperlinks by manually setting their .Visible property and another is recursively traversing the control tree.

 

The idea presented here is to hide all the hyperlink controls or for that matter change any property of a custom WebControl by raising an event from the page. In other words, the custom WebControl will subscribe to a custom page event and take action based on that event.


We are going to create a custom WebControl – HelpLink - that subscribes to a custom page event. Our page will check to see if there are any subscribers to this custom page event and notify them. We first define an interface that will hold all the events we want. We are using an interface because the HelpLink control has no way of knowing what custom events the page might raise. The HelpLink control will check to see if its containing page implements the interface and subscribe to the events. Note that the page that wishes to raise events will have to implement this interface. We have defined an interface called IVisible and added the events we wish to raise.

 

 

namespace WebControlLibrary

{

       public interface IVisible

       {

              event EventHandler ShowHelp;

              event EventHandler HideHelp;

       }

}

 

The code below shows the HelpLink custom WebControl that inherits from the HyperLink WebControl. We check to see if the containing page implements the IVisible interface and subscribe to the events. We then define event handlers that hide or show the HelpLink based on the event raised.

 

namespace WebControlLibrary

{

       [DefaultProperty("Text"),

              ToolboxData("<{0}:HelpLink runat=server></{0}:HelpLink>")]

       public class HelpLink : System.Web.UI.WebControls.HyperLink

       {

       // if the containing page implements IVisible, subscribe to the events

              protected override void OnInit(EventArgs e)

              {

                     if (Page is IVisible)

                     {

                           ((IVisible) Page).HideHelp += new EventHandler(HelpLink_HideHelp);

                           ((IVisible) Page).ShowHelp += new EventHandler(HelpLink_ShowHelp);

                     }

                     base.OnInit (e);

              }

 

              private void HelpLink_ShowHelp(object sender, EventArgs e)

              {

                     this.Visible = true;

              }

 

              private void HelpLink_HideHelp(object sender, EventArgs e)

              {

                     this.Visible = false;

              }

       }

}

 

Finally, here is the test page which implements IVisible. There are three HelpLink controls and two buttons. Clicking on Button1 raises the ShowHelp event which shows all the HelpLinks and Button2 raises HideHelp which hides all the HelpLinks.

 

       public class WebForm1 : System.Web.UI.Page, WebControlLibrary.IVisible

       {

              protected WebControlLibrary.HelpLink HelpLink1;

              protected WebControlLibrary.HelpLink HelpLink2;

              protected WebControlLibrary.HelpLink HelpLink3;

              protected System.Web.UI.WebControls.Button Button1;

              protected System.Web.UI.WebControls.Button Button2;


              public event System.EventHandler ShowHelp;

              public event System.EventHandler HideHelp;


 

              // ------
              // ------

              // ------

      

              private void Button1_Click(object sender, System.EventArgs e)

              {

                     OnShowHelp(EventArgs.Empty);

              }

 

              private void Button2_Click(object sender, System.EventArgs e)

              {

                     OnHideHelp(EventArgs.Empty);

              }

 

                           

              // If there are subscribers to ShowHelp, notify them

              public void OnShowHelp(EventArgs e)

              {

                     if (ShowHelp != null)

                           ShowHelp(this, e);

              }

 

              // If there are subscribers to HideHelp, notify them

              public void OnHideHelp(EventArgs e)

              {

                     if (HideHelp != null)

                           HideHelp(this, e);

              }

       }

 
Other Sample Codes

 

 

Raj Kaimal

Published Friday, April 09, 2004 12:02 PM by rajbk
Filed under:

Comments

# re: Raising events in the Page and handling it in a WebControl

It's a great article. Thanks
Note: Author assume reader(s) know how to create WebControl and WebForm; integrating WebControl in WebForm.

Friday, May 14, 2004 12:42 PM by HarrisonK

# re: Raising events in the Page and handling it in a WebControl

Good article! It gave me a fresh look on event handling and the simplicity of it, just what I was looking for! Thanks.

Friday, May 14, 2004 6:11 PM by NanderC

# re: Raising events in the Page and handling it in a WebControl

Thanks so much, beautiflly done!!!

Wednesday, May 19, 2004 11:47 AM by Yao Liu

# re: Raising events in the Page and handling it in a WebControl

Awesome, Keep it up!

Thursday, May 20, 2004 7:03 AM by Nik

# Very tidy example of custom events.

Well done, Raj.

Tuesday, June 08, 2004 3:32 AM by SomeNewKid

# re: Sending events from user controls to the parent containers

Wednesday, April 06, 2005 12:02 PM by TrackBack

# re: Raising events in a Page and handling it in a WebControl

Well done. It's a great article.

Tuesday, January 09, 2007 4:27 PM by Jan

Leave a Comment

(required) 
(required) 
(optional)
(required)