AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=63&pId=-1
Base Page and User Control Classes
page
by Robert Boedigheimer
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 43158/ 82

Background

By default, new web forms (.aspx) that are added to an ASP.Net project derive from the class System.Web.UI.Page, and new user controls (.ascx) derive from System.Web.UI.UserControl. These classes both provide many useful properties, methods, and events that can be utilized in our own pages and user controls. A useful technique to employ for all ASP.Net web sites is to create a custom class for all pages and user controls for a web site. The idea is to create a class that derives from System.Web.UI.Page, and then to change all the pages on a web site to derive from this new class. This provides a single place where behavior can be altered for all pages on the web site. The concepts are identical for user controls, so this article will focus on the System.Web.UI.Page class, with the same technique applying to the System.Web.UI.UserControl class as well.

At first this seems like just another way to provide common code for a web site, such as just using another assembly or another class in the same project. The difference, however, is that funtionality can be added to the single base class, and it affects how all other pages on the site behave without any other changes required. One example where using this technique proved very useful was when we had some issues with the default client side viewstate and were able to convert to use server side viewstate by only making changes to the custom base page class rather than all the web pages (see Server Side Viewstate for details).

Implementation

First it is necessary to create the new custom base page class, just create a new class file and derive from System.Web.UI.Page (the other code here is for the sample described):
public class CustomBasePage : System.Web.UI.Page
{
  //Example variable that can be set for all pages to access
  private bool bPrintView = false;

  public bool PrintableView
  {
    get
    {
      return bPrintView;
    }
  }

  public CustomBasePage()
  {
  }

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

The key for this is to add the : System.Web.UI.Page after the class name. This specifies that the newly created class will derive from the existing System.Web.UI.Page class, so that all of the existing properties, methods, and events will still be available to our pages. The example shows one potential use of the custom base class, where it overrides the OnInit() event to check for a query string parameter. It then sets a local variable and provides a property PrintableView that our pages can access to see what the querystring parameter held. This method provides a centralized place where the custom class can get the proper value (with try/catch for safety) and yet every page can access the property to get the value.

Originally all of the pages on the site derived from System.Web.UI.Page like this:
public class WebForm1 : System.Web.UI.Page

Now they need to be changed to derive from the custom class like this:
public class WebForm1 : CustomBasePage

Conclusion

There are many opportunities to leverage a custom base class for pages or user controls. Establishing the rule that this technique will be put in place for all ASP.Net sites provides great flexibility in the event that some functionality must be added to every page or user control.

Send comments or questions to robertb@aspalliance.com.


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-05-26 5:29:03 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search