Create Page Templates
Use page templates to apply a consistent look to your Web site.
by Jonathan Goodyear
I often judge a Web site's quality by the consistency of its user interface. As a user, once I learn the features a Web site has to offer, I want them to be available at all times, in the same place on the screen. As a developer, I don't want to be bogged down by repetitive page layout code that takes me away from the business problems I'm trying to solve. Centralized page layouts are the best way to solve both these issues.
VB.NET, ASP.NET, Internet Information Server (IIS) 5.0, .NET Framework SDK, Visual Studio .NET (optional)
Before ASP.NET, in traditional Active Server Pages (ASP) Web applications, you implemented page layouts using "include" files that contained the overall Web site structure. The layout "include" file made function calls to the ASP page, implementing the layout file to fill in the appropriate regions of the page. This approach had performance tradeoffs, but it made site maintenance a breeze.
Fast-forward to ASP.NET. "Include" files are no longer in style. User controls are the .NET way to reuse user interface elements. The user control approach, however, has a major caveat. User controls are fantastic for encapsulating islands of user interface elements, but they do little to help you build the overall page structure for your Web site. The solution: a custom page template.
A typical ASP.NET page inherits from a code-behind class, which in turn inherits from the Page class of the .NET Framework's System.Web.UI namespace. A code-behind class contains the implementation code for an ASP.NET page. The rules of inheritance implemented in the Common Language Specification allow you to insert a layer of abstraction between the Page class and your ASP.NET page's code-behind class. The abstraction layer you implement is your custom page template (see Figure 1). The Common Language Specification defines the rules that govern how classes in the .NET Framework interact with one another, and it has been submitted to the European Computer Manufacturers Association (ECMA) standards body.
Build a Custom Page Template
Building a custom page template is easy. Create a text file named PageTemplate.vb and declare a class that inherits from the Page class (see Listing 1). (Note: The code in this article is in VB.NET, but you can download the code project in C# from the VSM Web site; see the Go Online box for details.) Next, declare PlaceHolder objects as fields to hold the custom content regions that your ASP.NET pages insert into your page template. The PlaceHolder class is one of the intrinsic ASP.NET server controls. You want to allow the code that implements your template to apply only a single title to your page, so implement a write-only property procedure for the title field. In the procedure, clear out the PlaceHolder object's Controls collection before adding the specified title. This limits your Web page to a single title.
Back to top