Have you ever wanted to make an ASP.NET base page that controls execution of any aspx that inherits from it? Like controlling exactly WHEN the Page_Load logic occurs? Maybe logging the beginning and ending of the Load method. I’ve come up with a way to do exactly that. I’m not claiming that this is a unique idea, but it’s a very slick way to track and control the execution of all derived pages.
1: protected new event EventHandler Load;
2: private void RaiseLoad(EventArgs e)
4: if(Load != null)
5: Load(this, e);
7: private void Page_Load(object sender, System.EventArgs e)
9: Trace.Warn(“Begin Load”);
11: Trace.Warn(“End Load”);
15: base.Load+=new EventHandler(Page_Load);
Notice that I’m using the “new” keywork on my event. This hides the real event and creates another of my own. This method will trap the Load event from System.Web.UI.Page and then raise a new Load event that it completely controls. It can do some work before and after this happens. Now any aspx that derives from this will be executing within this framework. When it subscribes to the Load event, it will be subscribing this this class’s load event. You can also do this with the other events of the Control class.