Welcome to AspAdvice Sign in | Join | Help

Ultimate ASP.NET Base Page Class

I'm working on a base page for a new application and it seems like this is about the 5th or 6th time I'm building one of these things, so it's getting a bit repetitive.  In this case, it's for an ASP.NET 2.0 app, but I have base pages in use in 1.x apps as well, where they are arguably more useful (or at least, lacking built-in master page support, they are more necessary).  I'd like to know what app-agnostic features folks are including in the BasePage classes so that I can compile sort of the ultimate base page class (perhaps I'll call it UltimateBasePage) which inherits from and extends System.Web.UI.Page and the features of which are generally useful in most applications.  Probably we'll want a 1.x and a 2.0 version.

By way of example, a useful BasePage property is the RunTimeMasterPageFile, a tip from Dan Wahlin in the MVP ASP.NET 2.0 Hacks book (and also from ScottGu I just discovered).  Basically the property allows nested master page using pages to still benefit from Design mode in VS 2005 (though they don't get WYSIWYG master page support).  The code for this is

 

    private string runtimeMasterPageFile;

 

    public string RuntimeMasterPageFile {

        get {

            return runtimeMasterPageFile;

        }

        set {

            runtimeMasterPageFile = value;

        }

    }

 

    protected override void OnPreInit(EventArgs e) {

        if (runtimeMasterPageFile != null) {

            this.MasterPageFile = runtimeMasterPageFile;

        }

   

        base.OnPreInit(e);

    }

Another example property would be PrintView, as Robert B writes about here.  The code for this might look like:

  //Example variable that can be set for all pages to access
private bool bPrintView = false;

public bool PrintableView
{
get
{
return bPrintView;
}
}

override protected void OnInit(EventArgs e)
{
//Always call the base method when override
// so what it originally did can still happen
base.OnInit(e);

//Example that just reads a querystring that
// should be supported for all pages
if (null != Request.QueryString["Print"])
{
//Use try/catch in case value is not a boolean
try
{
bPrintView = Convert.ToBoolean(Request.QueryString["Print"]);
}
catch
{
//Do nothing, just leave as false
}
}
}
While scouring the Internet for useful UltimateBasePage additions, I also found one example of what NOT to do, here.
Do not, for the love of scalability, ever, open up a connection in every one of your pages in Init and then (hopefully) close it in Unload(). Bad Bad Bad.
 GridViewGuy has some nice suggestions, but they don't quite seem general enough to include in UltimateBasePage.
Another fairly common 1.x inclusion in the BasePage class was a ConnectionString property, but in 2.0 this is less necessary given the full support
for Connection Strings in System.Configuration, and also because DataSourceControls make it less necessary to write data binding code
(which should be done in a Data Access Layer anyway).

 A useful diagnostic method to consider including is a ViewState trace, found here.  Looks like this:

protected override void OnPreRender(EventArgs e)
{
object ViewState = HttpContext.Current.Request["__VIEWSTATE"];
if(ViewState == null)
{
HttpContext.Current.Trace.Warn("ViewState Size", "0");
}
else
{
HttpContext.Current.Trace.Warn("ViewState Size",
HttpContext.Current.Request["__VIEWSTATE"].Length.ToString());
}
base.OnPreRender(e);
}

 One last thing that might be worth adding to UltimateBasePage would be Google Analytics support, like Tim Haines describes:

protected override void Render(HtmlTextWriter writer )
{
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

    base.Render(htmlWriter);

    string html = stringWriter.ToString();

    if (Global.GoogleAnalyticsAccountNumber != "")
    {
        string GoogleAnalyticsScript = String.Format(ResourceManager.GetResource("GoogleAnalyticsScript"), Global.GoogleAnalyticsAccountNumber);
        int BodyCloseTagStart = html.IndexOf("</body>") -1;
        if(
BodyCloseTagStart >=0)
        {
            html = html.Insert(
BodyCloseTagStart, GoogleAnalyticsScript);
        }
    }
    writer.Write(html);
}

Published Thursday, September 14, 2006 11:59 PM by ssmith
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Ultimate ASP.NET Base Page Class

Some I use: HideFromRobots is a property that can be set in your page class that outputs a <meta name="robots" content="noindex,follow">. A MetaTags collection is handy too.

Friday, September 15, 2006 6:23 AM by James Shaw

# re: Ultimate ASP.NET Base Page Class

I think you forget about the capability of changing theme. I am using my basewebpage for that purpose. The could be nearly the same you use for master page

Friday, September 15, 2006 8:26 AM by vikram

# re: Ultimate ASP.NET Base Page Class

I like the part about the viewstate size. I am about to go through our whole site and work and track down bloated viewstates that might be slowing down our web application. Thanks for the tip!

Friday, September 15, 2006 8:45 AM by Elijah Manor

# re: Ultimate ASP.NET Base Page Class

For doing injection like the Google Analytics bits, I've preferred doing an HTTPModule similar to:

http://clariusconsulting.net/blogs/kzu/archive/2006/06/08/InstantGoogleAnalytics.aspx

- Aaron

Friday, September 15, 2006 3:16 PM by Aaron Robinson

# re: Ultimate ASP.NET Base Page Class

One note, the ViewState counter here only works on pages that are arrived at via a PostBack.  The initial view of the page will show the ViewState as 0, since the way it's reading it is from Request[].  I'm guessing it should be possible to parse the output in PreRenderComplete or Render in order to count how big the ViewState string is, though, using some code similar to the Google Analytics code above.

Friday, September 15, 2006 5:28 PM by ssmith

# re: Ultimate ASP.NET Base Page Class

Another good suggestion is the use of a RequireSSL property on the page.  Here's a nice overview of how to implement this:

http://weblogs.asp.net/kwarren/archive/2005/07/08/418541.aspx

Wednesday, May 23, 2007 3:57 PM by ssmith

# Finding Untitled Page Titles

Sadly, Microsoft decided that with the addition of the Title attribute on the @Page directive, it would

Tuesday, January 29, 2008 12:05 PM by Steven Smith

# Three Requests for ASP.NET 4 and VS 2010

I have three things that have been on my wish list for ASP.NET and/or Visual Studio that I'm curious

Sunday, March 16, 2008 4:25 PM by Steven Smith

# Show Page Load Time

When testing performance for an individual ASP.NET page, it&#39;s often useful to be able to see how

Monday, July 07, 2008 12:22 PM by Community Blogs

# Recursive FindControl

I&#39;ve been asking for a recursive FindControl() method as a method off of System.Web.UI.Control for

Monday, September 22, 2008 10:17 AM by Community Blogs

# Recursive FindControl

I&#39;ve been asking for a recursive FindControl() method as a method off of System.Web.UI.Control for

Monday, September 22, 2008 9:57 PM by Readed By Wrocław NUG members

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below