CODEDIGEST
Home Articles CodeDigest Tutorials InstallShield FAQs
Skip Navigation LinksHome » Article » ASP.Net Article » Using Custom BasePage Class in ASP.Net  Submit Articles and Win Geeky Prizes!!   You are not logged in.
Search
 

Sponsors
InstallShield
 

Product Spotlight
 

Technologies
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Using Custom BasePage Class in ASP.Net
Free Trial: InstallShield 2010 for Windows Installers Is InstallShield right for you? InstallShield handles your most complex installation requirements in minutes. Try it now.

By Satheesh Babu
Posted On Mar 26,2008
Article Rating: (Login)
Average Rating: 4
No of Ratings: 1
No of Comments: 10
Category: ASP.Net
Print this article.

Subscribe to our feed!

Using Custom BasePage Class in ASP.Net

 

Every Code Behind class of ASP.Net pages will inherit System.Web.UI.Page Class by default.

Refer the below code.

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

{

}

 In this inheritance hierarchy we can include a custom BasePage class that inherits System.UI.Page class and in turn make the codebehind class to inherit the BasePage Class.

InstallShield

 

Why should we do this?

There will be some requirements in most of our projects that has to repeat same operations on every page. This will make us to repeat the same code in every page and thus making it redundant. Here comes the use of introducing our own custom BasePage class in the codebehind class inheritance hierarchy. By including a custom BasePage class we can put all the repetitive operation in the BasePage and hence we can reuse it on every page. This article will help you to understand the implementation custom BasePage class in a simpler way with some example scenarios. 

 

How to achieve this?

Include a class called BasePage that derives System.Web.UI.Page class as shown in the below code.

BasePage Class

public class BasePage : System.Web.UI.Page

{

protected override void OnLoad(EventArgs e)

    {

       base.OnLoad(e);

     }

 }

 

Now, make the codebehind classes to inherit our custom BasePage class.

 

CodeBehind Class

public partial class Register : BasePage

{

}

Moving forward we will see some of the common scenarios where we can use the BasePage class.

 

Custom Error Handling

One way of handling the errors in ASP.Net page is using the Page_Error Event. This event can be used to seize the error that is occurring in page level. i.e. including a Page_Error event in codebehind class will make the ASP.Net to call the event whenever any error occurred on the page.

 

void Page_Error(object sender, EventArgs e)

    {

//Log to EventLog or Sql Error log table     

 Server.Transfer("Error.aspx");      

    }

 

In this scenario, we can use the BasePage and declare the event in the BasePage class and thus we can prevent the redundant code in every codebehind class. Our BasePage class will now look like,

 

public class BasePage : System.Web.UI.Page

{   

        protected override void OnLoad(EventArgs e)

        {

            base.OnLoad(e);

        }

    void Page_Error(object sender, EventArgs e)

    {

       Server.Transfer("Error.aspx");      

    }

}

 

If you need to log or display a detailed technical error information on the error page then make sure you are using Server.Transfer() instead of Response.Redirect() because only then you can use Server.GetLastError() method to get the error information on the error page. It is because Response.Redirect() will make redirection to Error.aspx as a new request and hence it will not have error information that is occurred on the page which will be actually previous request in this scenario.

 

Sponsors

Useful Books For Developers
ASP.NET 3.5 Social Networking More books..

Similar Articles

Making Meta Tags with BasePage

With the introduction ASP.Net 2.0 we can set the Meta tag from the codebehind file using HtmlMeta class. Instead of repeating the code that adds the Meta tag on every page we can move this part to BasePage class. A very good approach will be a exposing a public property in BasePage and setting the Meta tag from the @Page directive.

 

Our BasePage will be,

public class BasePage : System.Web.UI.Page

{

 

    private string _metakeywords;

    private string _metadescription;

 

    public string MetaKeywords

    {

        get

        {

            return _metakeywords;

        }

        set

        {

            _metakeywords = value;

        }

    }

    public string MetaDescription

    {

        get

        {

            return _metadescription;

        }

        set

        {

            _metadescription = value;

        }

    }

     protected override void OnLoad(EventArgs e)

        {

            if (!String.IsNullOrEmpty(MetaKeywords))

            {

                HtmlMeta tag = new HtmlMeta();

                tag.Name = "keywords";

                tag.Content = MetaKeywords;

                Header.Controls.Add(tag);

            }

 

            if (!String.IsNullOrEmpty(MetaDescription))

            {

                HtmlMeta tag = new HtmlMeta();

                tag.Name = "description";

                tag.Content = MetaDescription;

                Header.Controls.Add(tag);

            }

            base.OnLoad(e);

        } 

      

}

 

To make this work we need to include CodeFileBaseClass="BasePage" attribute in @Page directive in the ASPX. This is done because in visual studio the codebehind file is referenced through CodeFile attribute which has the path of the code and this attribute is used to specify the actual class name. Now we can specify the Meta keywords and descriptions in @Page directive like,

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFileBaseClass="BasePage" CodeFile="Default.aspx.cs" MetaKeywords="ASP.Net,CSharp" MetaDescription="BasePage implementation in ASP.Net" Inherits="_Default" %>

 

This will add Meta tags to every page when it is rendered. Besides this you can also declare some constants that you require on every page of your projects.

Downloads

Download Source

 

Conclusion

 Thus we learnt the implementation of BasePage in ASP.Net and some real time scenarios where we can use it. Download the source code attached with this article and see it in action.

Happy Coding!!

You can contribute to CodeDigest.Com:
Donate to CodeDigest.com
Article Feedback
Title  
Submitted By  
Comment  
Enter the verification number
 
Comments
Nicely described
@ Satheesh, Nice article - short and sweet. One comment I have is to recommend the use of automatic properties in your code. In this way the code: private string _metakeywords; public string MetaKeywords { get { return _metakeywords; } set { _metakeywords = value; } } Becomes: public string MetaKeywords { get; set; } @ JiltedCitizen, Yes. This is just an example of Inheritance, which may be applied to any class in the hierarchy.
RE: Design time support
Hi Vladimir, Custom property can be added, refer my meta tag code in the article. To my knowledge it is not possible for the to pick the basepage class automatically when adding a new form..
Design time support
To me, the next question is: how to add VS design time support to a custom page base class? I would like to be able to 1) Add a custom property to BasePage. 2) Pick BasePage Web form instead of a regular Web Form when adding a new page in VS. 3) Set a value of that custom property in VS at *design time*. Any links / suggestions would be highly appreciated (vkelman at gmail dot com) http://www.pro-thoughts.blogspot.com
Nice dude
Nice article. Short and Sweet
good
nice article.. articulation is easier to read and understand.
RE: Is this for Asp.Net 1.1 ?
I found the link, http://weblogs.asp.net/scottgu/archive/2005/08/02/421405.aspx
RE: Is this for Asp.Net 1.1 ?
Guys, Thats correct!This feature is more useful in ASP.Net 1.1 than 2.0 because we can achieve this through master page for most of the things. But still things like using the meta tags in page directive will still hold good in ASP.Net 2.0 to have BasePage. I remember scott guthrie blogging about the similar thing before. will get the link if possible. Also, If we have more than one master page(But not always will have this requirements) in our project then we can think of using the basepage for not repeating the codes in master pages.
Is this for Asp.Net 1.1 ?
Hi, I remember using this with 2003 & 1.1. Can you specify how its different from using Master Pages in 2.0 ?
Master Pages
Is this functionality not better served with a master page?
user controls?
This won't also apply to user controls will it?