Tuesday, June 3, 2008

Perfect ASP.NET application, Part 3

clsStandardPage

Our goal here is to write easy maintainable application. So nowadays in any of my projects I define following class (we will discuss later what is clsBrowser and why i need it.)

public class clsStandardPage : System.Web.UI.Page
{
    public clsBrowser _objBs;

    protected override void OnPreInit(EventArgs e)
    {
        //no caching
        Response.CacheControl = "no-cache";
        Response.Expires = -1;
        //Get Browser object
        _objBs = (clsBrowser)Session["browser"];
        if (_objBs == null)
            Session["browser"] = _objBs = new clsBrowser();
        base.OnPreInit(e);
    }
}

In web.config we do a small change

<system.web>
    <pages pageBaseType="clsStandardPage" />
</system.web>

And now all our pages derived from clsStandardPage. 

Benefits: All our pages now derived from clsStandardPage and we can change the way they behave in one place (You will see how later). 

Notes: You can have more than one clsStandardPage, but then on a page you will need to specify (if you do not separate code and html)

<%@ Page Inherits="clsStandardPage1"%> 

to overwrite the web.config setting. And if you do code separation (VS default) you would need to modify your .cs class. Something like

public partial class Default2 : clsStandardPage

 

Read next post to find out what is clsBrowser and why it's so much better to have clsStandardPage

PS: If you are working on your home page you are welcome to do anything you want. If you working on a real project even small one use MasterPage. First of all small projects tend to become medium projects with time. And trust me it's much easier to modify a navigation, menu or simply change company's logo in one place than 10s aspx pages.

Continue >>

No comments: