ASP.NET base class refined for total control – level 300

In my last post, I wrote about creating an ASP.NET base class that is in complete control of its execution without the possibility of a derived page executing code when it shouldn’t.  With OOP, we can’t guarantee this if we are exposing virtual methods, but what I have done is laid a framework for developing aspx’s on top of this base page.  Some of my base page is below:

        protected Page()

{

base.Init +=new EventHandler(Page_Init);

base.Load +=new EventHandler(Page_Load);

base.PreRender +=new EventHandler(Page_PreRender);

base.Unload +=new EventHandler(Page_Unload);

}

public new event EventHandler Init;

protected new virtual void OnInit(EventArgs e)

{

Trace.Warn(“Begin Base OnInit”);

if(Init != null)

Init(this, e);

Trace.Warn(“End Base OnInit”);

}

private void Page_Init(object sender, EventArgs e)

{

Trace.Warn(“Begin Base Init”);

OnInit(e);

Trace.Warn(“End Base Init”);

}
Notice that I use the “new” keywork for OnInit as well.  This is so that if the aspx overrides the OnInit method, it still cannot execute code before the base page gets a chance to.  The base page reacts to the Init Event from its parent, executes some code, then calls its OnInit method (which raises this class’s Init event).  The aspx subscribes to its base class’s Init event.  The end developer experience is exactly the same, but what I do in my base class results in complete control of page executiong most of the time.  Most of the time the aspx will override On* methods and execute event handlers.