Page Templates: Introduction
page 1 of 1
Published: 07 Oct 2003
Unedited - Community Contributed
Abstract
Nearly every web site has some type of common layout used for each Page. Classic ASP allowed developers to use Include files to encapsulate reuse. ASP.NET provides many new and better techniques to accomplish the same. This article provides a brief introduction to Page Templates in ASP.NET, which generally refers to the use of inheritance with a base Page class.
by Paul Wilson
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 56019/ 117

Page Templates: Introduction

Page Templates: Introduction

Paul Wilson
www.WilsonDotNet.com
www.ASPAlliance.com/PaulWilson

Article Index            Download Demo            Next Article

Overview

Nearly every web site has some type of common layout used for each Page. Classic ASP allowed developers to use Include files to encapsulate reuse. ASP.NET provides many new and better techniques to accomplish the same. This article provides a brief introduction to Page Templates in ASP.NET, which generally refers to the use of inheritance with a base Page class.

User Controls

The easiest new Page Template method in ASP.NET involves User Controls. User Controls are mini-Pages, or Pagelets, created almost just like Pages. Most developers find it easy to setup the regions common to the whole site into User Controls, like a Header and Footer, possibly a Menu and SideBar. This Page Template technique simply allows one to automate it on each Page, instead of registering and using these User Controls manually each time. It was originally suggested by some to use a PlaceHolder for the Content, but further work has demonstrated the Content is best left on each Page. See the code download or my Page Template Generator for the full examples.
Listing 1: User Controls
  public class BasePage : System.Web.UI.Page
  {
    private string pageTitle;
    public string PageTitle {
      get { return pageTitle; }
      set { pageTitle = value; }
    }
    
    protected override void OnInit(System.EventArgs e) {
      this.Controls.AddAt(0, LoadControl("Header.ascx"));
      base.OnInit(e);
      this.Controls.Add(LoadControl("Footer.ascx"));
    }
  }

Direct Render

The best performing Page Template method in ASP.NET avoids all controls, since each control is about 30% slower than the equivalent static html. Note that ASP.NET is still usually a good deal faster than Classic ASP, due to compiling and better caching, so most developers don't bother here, since this technique does have the reputation of being harder to utilize. The difference is that the common layout must be directly coded as html, without using the nice designer (i.e. control) features of GUI Designers, although a quick look at the code and you realize its not that different. See the code download or my Page Template Generator for the full examples.
Listing 2: Direct Render
  public class BasePage : System.Web.UI.Page
  {
    private string pageTitle;
    public string PageTitle {
      get { return pageTitle; }
      set { pageTitle = value; }
    }
    
    protected override void Render(System.Web.UI.HtmlTextWriter writer) {
      writer.Write("HEADER");
      base.Render(writer);
      writer.Write("FOOTER");
    }
  }

Other Resources

This article is meant to just be a brief introduction to Page Templates. The following comprehensive list of articles/tools, by myself and others, will provide the means for you to take and research this subject further.

Author Bio

Paul Wilson is a software architect in Atlanta, currently with PRG-Schultz. He specializes in Microsoft technologies, including .NET, C#, ASP, SQL, COM+, and VB. His WilsonWebForm Control allows Multiple Forms and Non-PostBack Forms in ASP.NET. He is a Microsoft MVP in ASP.NET and is also recognized as an ASPInsider. He is a moderator on Microsoft's ASP.NET Forums, as well as one of the top posters. He is holds the MCSD, MCAD, MCDBA, and MCSE certifications. Please visit his website, www.WilsonDotNet.com, or email him at Paul@WilsonDotNet.com.


User Comments

Title: uyuy5u   
Name: yuy
Date: 11/29/2012 6:22:17 AM
Comment:
tyuy
Title: .NET   
Name: gershom
Date: 2/24/2008 3:57:17 AM
Comment:
very nice to read it...thnx..
Title: About Page Template   
Name: Uha Rani Dash
Date: 4/26/2006 7:51:49 AM
Comment:
The article is really very good.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 11/28/2014 4:32:37 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search