Now, for those who have stayed. As you know ASP.NET is Microsoft's web application language, built around the .NET framework. And as you probably also know, the term "basepage" was derived from an Object Oriented Programming (OOP) term. "Base class", is a term used to describe the highest level of inheritances within a OOP structured program. In other words, if there was a class called "circle" it probably inherits from the "shape" class. The problem is, that metaphor doesn't translate well to the web world. A web page, is a web page, right? Ok, if you are advanced you will quickly say, "a web page is not just a web page", and you would be right, it can have lots of sub objects associated with it, but for this example we are just looking at the pages as plain pages; the very beginnings of a site.
So, Why BasePage?
BasePages are useful because they allow you to implement functionality globally across a site. This is good for all sorts of reasons, but one would be security. If you have site that is password protected you will want to check to see if the person viewing a particular page has been authenticated, or to ensure their session is valid. This can be done within the BasePage, instead of copying and pasting the same code to every page. (Note: There are these things called HTTP handlers that work like this at a lower lever, but we are not talking about that right now.)
Ok, how do I do it?
When you "Add" a new WebForm.aspx page to a website project in visual studio, it automatically assigns "System.Web.UI.Page" as the default base class. This is convenient. It allows you to start coding immediately (Note: There are ways to override this, not discussed in this article.) . The issue arises when you want to have a particular piece of code fire on every page. Because "System.Web.UI.Page" can not be modified (because its a compiled Assembly), you would have to copy the code, you need to fire on every page, to each page. This is not good. What we need to do is to create a custom BasePage, an intermediary between the "System.Web.UI.Page" and your WebForm.
So instead of this:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
You have this:
public partial class _Default : CustomBasePage
{
protected Page_Load(object sender, EventArgs e)
{
}
}
So lets get started, how do you create this intermediary class? Well its easy.
- Add a new class your website project and call it BasePage.cs.
- When the newly created file opens it will look like this:
///
/// Summary description for BasePage
///
public class BasePage
{
public BasePage()
{
//
// TODO: Add constructor logic here
//
}
} - To turn this ordinary class into a useful BasePage, your going to have to modify a few things. One is your going to have to inherit from our friend "System.Web.UI.Page". This allows all the meat of that framework Assembly to be available to your web page. This is done by adding a " : " Powell after the class name and adding the Assembly reference. Like this: public class BasePage : System.Web.UI.Page
- Next we want to add the page life cycle events that will fire for each page. These are the methods where you will put your code that you want to fire on every page. Like this:
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
}
protected override void OnPreInit(EventArgs e)
{
//Add the code you need to fire here
base.OnPreInit(e);
}
protected override void OnLoad(EventArgs e)
{
//Add the code you need to fire here.Response.Write("Hello");
}
base.OnLoad(e);
} Now that you have created your BasePage class, change the inherited class on your WebForm to your newly created BasePage.
- Compile your website and view it. The word "Hello" should be at the top of the page.
Congratulations...You have just created a BasePage.
Summary
In this article I briefly and generally described the how and why of using a custom BasePage within your website. I hope you find it useful.
Resources:
- ASP.NET 2.0 Website Programming: Problem - Design - Solution
by Marco Bellinaso
Wrox Press © 2006 - http://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx
No comments:
Post a Comment