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
------------------
[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
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