Templates provide an excellent way to maintain a
consistent look and feel across a Website. However, version 1 of ASP.NET
doesn’t natively support the concept of templates. This isn’t a major
stumbling block since the .NET platform provides a robust object-oriented
environment that can be leveraged to create custom template solutions.
Many different ASP.NET template solutions have been created that leverage the
concept of inheritance in .NET.
I was recently tasked with creating an ASP.NET
template framework that was very flexible as well as useable by programmers and
non-programmers alike. I researched several existing template frameworks
and found that each had definite pros (and some had a few cons) depending upon
the skill level of the developer that would be using them. A few of the
template solutions I came across are listed below (the links are not listed in
any particular order):
From a programmer’s perspective, many of the template
concepts listed above (as well as others that are not listed) could be used
since they offer robust solutions. However, from the non-programmer
perspective (think of a “Web publisher” who only knows the basics of HTML) many
of these would be tough to comprehend making it difficult to integrate a single
Web page into a Website’s template framework.
As a result of needing to support a variety of skill
levels, the ASP.NET template framework I created relies upon XML to mark-up the
structure of a given Website. This allows the creator of the template to
generate the template 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). An example XML template is shown below. Looking through
it you’ll see that it mainly consists of standard HTML tags.
XML for ASP.NET Developers
The template references ASP.NET UserControls that are
used with the <UserControl> tag (prefixed with the “template” local
namespace which is associated with the namespace URI of
http://www.xmlforasp.net/templates). The location where a given page’s
content should be placed within the template structure is identified through a
ContentPlaceHolder tag with an id attribute value of “Content”. This is the only
required tag in the template. Failure to include it will result in an
error.
The template framework relies upon a class named
BasePage that is responsible for locating the XML template file and parsing
it. ASP.NET pages that need to “inherit” the look and feel defined in an
XML template file will derive from the BasePage class. BasePage looks on
the querystring, in cookies, and in the web.config file for the template file
location.
As BasePage parses the template (using .NET’s
XmlTextReader class), it creates an object hierarchy that allows programmers to
easily access different parts of a page. This object hierarchy relies
upon classes named BaseHtml, BaseHead, BaseBody, and BaseForm. Existing
content found within a given page is automatically added into the proper place
within the template based upon the location of the PlaceHolder tag mentioned
earlier. Access to this content is made available through the BaseForm class’s
Content property.
For non-programmers, their content can automatically
be integrated into the template framework by adding either a form tag with the
runat=”server” attribute, or by adding the following directive at the top of
their page:
<%@ Page %>
So how does the BasePage class (and associated XML
template) get involved if that’s all the non-programmer adds into the
page? The developer of the Website simply has to add the following
element into the web.config file’s <system.web> tag to specify that the
BasePage class should be inherited from rather than the default
System.Web.UI.Page class:
Although this solution certainly isn’t the “end all” of ASP.NET template
solutions, it does provide a flexible and easy way to implement a template framework
that can be used by people of all skill levels. The entire XML for
ASP.NET Developers Website is based upon the XML template framework.
|