Vivek Thakur

Chaotically Complex

  Home  |   Contact  |   Syndication    |   Login
  105 Posts | 1 Stories | 504 Comments | 65 Trackbacks

News



Archives

ASP.NET Ventures

*******************
Note: view the updated entry below:

http://www.codeasp.net/blogs/vivek_iit/microsoft.net/159/base-page-class-in-asp.net

*******************

I have found using a Base Page class in my web projects immensely useful. What exactly is a Base Page class?

We know that all our web page code behinds derive from System.Web.UI.Page class as:

public partial class Default4 : System.Web.UI.Page

{ ...}

Now, many times we need to code the same thing in every page class, for example consider these scenarios:

1. Setting the culture of the current thread in the Page class's InitilaizeCulture() method so that all pages belong to the same user selected culture.

2. Redirecting the user to some “time out“ web page when the user Session has expired.

3. Getting the value of some control (like search textbox) which is in the MasterPage.

4. Setting the Meta keywords and description tags for search engine optimization.

5. Any other code which is common across most pages.

We can make our life easier by creating a base class and deriving all our pages from this class instead of System.Web.UI.Page class as:

// <summary>
///
BasePage for the common funtionality in all
///
the web pages of the site.
///
</summary>
public class BasePage:
System.Web.UI.Page
 
{
      //common code...
  }

Now all our other pages will derive from this class:

public partial class _Default : BasePage
{
    .///Code...
}

This will make our code more maintainable.

posted on Friday, October 06, 2006 11:30 AM

Feedback

# re: Base Page Class in ASP.NET 10/8/2006 6:49 PM Henrik Feldt
Yes, the page controller application design pattern.

What about the front controller pattern then? And about using Masterpages to facilitate those types of checks that you're talking about using BasePages. I'd rather use the MasterPage since it's more integrated in .Net 2.0. It also becomes MORE complex if you have a multitude of dynamic URLs to take care of; branching of logic...

I'm searching for a good front controller pattern to use with master pages. Feel free to respond to my email through my site.

# re: Base Page Class in ASP.NET 10/8/2006 10:40 PM Vivek Thakur
Hi Henrik,

I have blogged about the FC based design(see one of my ealrier posts) and I have faced many issues with a pure FC based desgin in ASP.NET. So I think a pure FC design offers no value. Regarding MasterPages, I do not want my ContentPages to depend on the MasterPage much and also I tend to avoid writing any logical/branching code in the master pages too.

Anyways, these comments are based on my personal experience only. There can be a good FC based design (with some ingenuity thrown in) but I am not aware of!

Thanks

Vivek


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: