Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Simplify Site Updates With Templates
Use a template to create a consistent look and feel for a Web site that doesn't require the end user who updates the site to be a programmer himself.
by Dan Wahlin

Posted January 20, 2004

Technology Toolbox: C#, ASP.NET, XML

Web developers have long faced the challenge of maintaining a consistent look and feel across a Web site. Several different types of approaches have been developed over time to meet this challenge, including the use of server-side include files, Cascading Style Sheets (CSS), XML combined with Extensible Stylesheet Language Transformations (XSLT), and ASP.NET user controls. All these solutions can get the job done, but their flexibility varies and none of them, apart from user controls, lets you leverage object-oriented programming principles.

ADVERTISEMENT

ASP.NET 2 (in alpha at the time this article was written) will address the challenge of maintaining a Web site's look and feel through specialized template files referred to as "master pages" (see my article on the new ASP.NET 2 features, "Maximize Productivity With ASP.NET," in Additional Resources). This feature holds a lot of promise, but it isn't available as a production-quality release to developers creating today's cutting-edge Web sites. Nevertheless, ASP.NET 1.1 and the current version of the .NET platform provide a robust object-oriented environment you can use to create custom template solutions.

It's not hard to find ASP.NET template frameworks that leverage the concept of inheritance in .NET (see Additional Resources for links to a few of the template frameworks available currently). Each framework has its own set of pros and cons depending upon the skill level of the developer who needs to use them. As a programmer, you might use a variety of the frameworks available for ASP.NET 1.1 because they offer robust functionality. However, the nonprogrammers who use the system—think of a "Web publisher" who only knows the basics of HTML—might find them tough to comprehend, making it difficult to integrate a single Web page into a Web site's template framework.

To support the variety of skill levels of those who might have to update Web pages, the ASP.NET template framework discussed in this article relies on XML rules to mark up the generic structure of a given Web site. XML lets the template's creator generate the structure quickly and easily without having to worry about embedding HTML code into .NET classes and recompiling and redeploying whenever the template changes. It also prevents unnecessary nesting of Web server controls within user controls (or user controls within user controls).

Using an XML template also means you can wrap Web page content automatically with the desired header, footer, and menus without writing any additional C# or VB.NET code. You can also present these same pages in alternate formats (such as "printable" pages) by changing templates dynamically. You can parse the XML template quickly and efficiently using .NET's XmlTextReader class—without degrading an application's performance.

Create a Template
Begin by creating a template that defines the generic structure of the site (see Listing 1). Note that this template consists mainly of standard HTML tags that follow the rules defined in the XML 1.0 specification.

The template defines where you should place page content by using an element named ContentPlaceHolder (named after the container control that will be used in ASP.NET 2.0's master pages). This element is prefixed with a local namespace prefix named template and associated with the namespace URI of www.xmlforasp.net/templates. If you're unfamiliar with XML namespace URIs, they help avoid naming collisions, and they work similarly to namespaces found in the .NET Framework (see Additional Resources).




Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home