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
Using inheritance with ASP.NET pages

I go back to work full time next Tuesday.  My terminal leave is almost up, I'm a civilian, and my new office furniture will be arriving today. :) 

Yesterday I was visiting with two colleagues of mine, and I was struck with a surprising reality:  They were programming with ASP.NET but had no idea that they were using inheritance.  One of the two was a former college professor of mine from whom I learned a great deal of database knowledge.  I pondered on it for a while, and I think that there are probably many out there that don't yet know how to use the power of inheritance to improve their .Net web applications.  I will now attempt to simply explain an easy way one can easily improve a page using inheritance.

First, when you write code in an ASP.NET page, you are writing code in a class that is inheriting from another class.  Whether it's specified in the machine.config, web.config or in the <@Page> directive at the top of the page, your page has to inherit from a class that ultimately will inherit from System.Web.UI.Page.  When you use the code-behind or code-beside technique, your page inherits from your code-behind class, which inherits from System.Web.UI.Page.  Look at the class.  Why else do you see “System.Web.UI.Page” in every code-behind page?  It makes your page contain the functionality of the Page class in the .Net framework.  It makes your page a System.Web.UI.Page object.  The Page object contains the properties for accessing Request, Response, Server, Cache, etc. 

Suppose you want to implement tracking for some of your pages?  You could put relevant code in every page, or make a static method in a shared class to track the current request.  But why not put this functionality in a class by itself, say “TrackedPage”.  When you declare TrackedPage, inherit from System.Web.UI.Page:

public class TrackedPage : System.Web.UI.Page{ }

and in your code-behind page, replace the reference to System.Web.UI.Page with “TrackedPage”.  This will make your page a “TrackedPage” object and will benefit from any logic you put in TrackedPage.  For instance,

public class TrackedPage :  System.Web.UI.Page{
protected override void OnInit(EventArgs e)
  {
   base.OnInit(e);
   //logic to track this request
   Trace.Write(“Tracking“, “This request has been tracked.“);
  }
}

will perform your tracking logic and write a line to the trace log for every page that inherits this custom class of yours.  You can imagine all the functionality you can include.  What if you have a custom user object that you need to be available for every request?  Make it a property of your class here, and make it the base page for all your pages? 

I find that a lot of people make user controls to encapsulate common functionality.  This is not a good technique.  Instead, put it in base pages and inherit from them.

My project on GotDotNet makes heavy use of this technique, and it's easy to extend EZWeb because all you have to do is inherit from Palermo.EZWeb.UIPage, and you have all the functionality available.


Posted 05-13-2004 12:46 PM by Jeffrey Palermo

[Advertisement]

Comments

JosephCooney wrote re: Using inheritance with ASP.NET pages
on 05-13-2004 4:15 PM
I think base pages are one of (possibly THE) best feature about ASP.NET.
Peter van Ooijen wrote re: Using inheritance with ASP.NET pages
on 05-14-2004 2:52 AM
They are great, but it is a pitty you don't inherit the markup. You only inherit the codebehind, not the aspx itself. You can drop controls on your base form but they will not show up in the inheriting page.

Jeffrey Palermo wrote re: Using inheritance with ASP.NET pages
on 05-14-2004 3:04 AM
Yeah, but all this is is class inheritance. I've been able to do some much since I've wrapped my brain around object-oriented concepts. This isn't unique to ASP.NET, it's just a benefit from the underlying object-oriented .Net Framework. From what I read about Master pages, you'll be able to inherit markup by nesting master pages.

Have you checked out my EZWeb Project? http://workspaces.gotdotnet.com/ezweb
Peter van Ooijen wrote re: Using inheritance with ASP.NET pages
on 05-14-2004 6:54 AM
Hi.
It is class inheritance. Asp.Net does even more. When it comes to serving the page it will generate a new class which includes members for all controls on the aspx and which includes all <%s > stuff like databindings in plain C#. This generated class inherits from your code behind class.

Before .net I used to do Delphi which was fully OOP from version 1. In there you could inherit visual (win) form classes, including controls. It could be pretty flaky but the idea worked. Sometimes I wished I could do that with an aspx (production) webforms as well. Have to wait till it is accepted to roll out 2005 apps. With masterpages.

In .NET I do use base (form) classes and I like it. Just like you. Havn't looked at you project (yet).

Peter
stefan demetz wrote re: Using inheritance with ASP.NET pages
on 05-15-2004 8:39 AM
Enterprise grade ASP.NET Development and code reuse
http://dotnetjunkies.com/WebLog/stefandemetz/archive/2004/01/14/5631.aspx
Shannon J Hager wrote re: Using inheritance with ASP.NET pages
on 05-17-2004 5:53 AM
I have EZWeb in the root of a test web and am running into the problem described here:
http://weblogs.asp.net/dneimke/archive/2004/05/17/133116.aspx
Jeffrey Palermo wrote re: Using inheritance with ASP.NET pages
on 05-17-2004 6:37 AM