Welcome to Bilal Haidar [MVP, MCT] Official Blog Sign in | Join | Help

BasePage and Page interaction in ASP.NET

I am focusing these days on covering the different design patterns available for developing Web Applications. A very nice technique I have learned.

Suppose you have several pages in a Web application, as you know we have several events that fire during the page-life cycle. What if you want to have some common code in any event fired during the page life cycle, for this example suppose it is Page_Load. At the same time, you need every page in the Web application also to add some code processing into the page_load event. How will you solve it?

A very nice technique that I personally like explained below:

Create the BasePage.cs as follows:

public class BasePage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Hello World From BasePage <br/>");

        PageLoad(sender, e);
    }

    virtual protected void PageLoad(object sender, EventArgs e)
    {
    }
}

Notice the virtual method defined in the BasePage class above. The reason it is virtual is to give each page in the Web application inherirting from this BasePage the chance to add additional code to be executed inside the Page_Load. As you can see also, I am calling the PageLoad inside the Page_Load method.

A page in the Web application can be implemented as follows:

public partial class _Default : BasePage
{
    protected override void PageLoad(object sender, EventArgs e)
    {
        Response.Write("Hello World from Page");
    }
}

In an ASP.NET page, I simply inherit from the BasePage and then override the PageLoad method!!

This way, when the page is requested, the Page_Load inside the BasePage is executed first, and implicitly the PageLoad is executed if it is implemented by the child page.

Hope you like this technique!

Thanks

 

Published Monday, September 24, 2007 8:54 AM by BilalHaidar [MVP]

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: BasePage and Page interaction in ASP.NET

Nice one Bilal

Tuesday, September 25, 2007 9:24 AM by Bilal Shouman

# re: BasePage and Page interaction in ASP.NET

Bilal,

If you have a BasePage which inherits from Page, just override OnLoad method and call the base.OnLoad in it. Similarly, every other class which inherits from your BasePage, can override OnLoad and call the base (which in turn executes the overridden OnLoad method in our BasePage). You don't need to create a new PageLoad method.

HTH...

Deepak

Saturday, October 13, 2007 9:44 AM by Deepak

# re: BasePage and Page interaction in ASP.NET

Hello Deepak,

Usually you want the base class to run some code that is needed on all pages.

To me, it is better if you let the base class control that! As I have shown my code, the page has no control over the code that should run by the base page!!

The page just does its work and it is the responsiblity of the base class to ensure "the common functionality".

what you are saying requires that on every page to call base.OnLoad!

Both works fine, but I prefer to make my base page have control over all pages!

Regards

Sunday, October 14, 2007 11:33 AM by BilalHaidar [MVP]

Leave a Comment

(required) 
required 
(required)