Tuesday, August 22, 2017
 
Code Search
 
Code Bank
 
.NET Applications
XSD Schema Generator
 
.NET/XML Training




 







XML-Based ASP.NET Page Templates
Description

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.

<html xmlns:template="http://www.xmlforasp.net/templates"> <head> <title>XML for ASP.NET Developers</title> <script language="javascript" src="Scripts/scripts.js"></script> <link rel="stylesheet" type="text/css" href="Style/style.css" /> </head> <body bgcolor="#ffffff" topmargin="0" leftmargin="0"> <form id="frmPageForm"> <table class="tableBorders" border="0" width="759" cellpadding="0" cellspacing="0" ID="Table1"> <tr> <td colspan="2" align="left" valign="top"> <template:UserControl id="ucHeader" src="UserControls/Header.ascx" /> </td> </tr> <tr> <td align="left" valign="top" width="164" bgcolor="#02027a"> <template:UserControl id="ucNavigation" src="UserControls/Navigation.ascx" /> <template:UserControl id="ucPartners" src="UserControls/Partners.ascx" /> </td> <td align="center" valign="top" width="595"> <template:ContentPlaceHolder id="Content" /> </td> </tr> <tr> <td colspan="2"> <template:UserControl id="ucFooter" src="UserControls/Footer.ascx" /> </td> </tr> </table> </form> </body> </html>

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:

<pages pageBaseType="XmlTemplate.WebControls.BasePage, XmlTemplate" />

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. 


Teaches XML, C#, VB.NET, XmlTextReader
Requirements .NET 1.1
Example: Click Here
View Source Code: Click Here
Download Code: Click Here

 


.NET, SharePoint and Silverlight Training Solutions
    Online, Onsite and Video Training on .NET and SharePoint technologies available through The Wahlin Group.



© 2007 Wahlin Consulting LLC
XML for ASP.NET Developers in bookstores
Get XML for ASP.NET
Developers