Jeffrey Palermo (.com)

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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. 

Posted 10-21-2004 4:06 AM by Jeffrey Palermo

[Advertisement]

Comments

Hasani wrote re: ASP.NET base class refined for total control - level 300
on 11-26-2004 8:02 AM
One thing I don't understand is why one cannot provide a default constructor when creating a ascx/aspx (user control/page).

If I were to use your base class, I would add the following viritual method.

protected virtual void OnConstructor();

and add the following line to the constructor of your base class.

OnConstructor();
Jeffrey Palermo wrote re: ASP.NET base class refined for total control - level 300
on 04-22-2005 4:54 AM
Thanks Jeffrey, this saved me some time.