29 MEMBERS HAVE Rated this Article. Result:
Popularity: 6.41. Rating: 4.38 Out of 5.
Download Source Files - 16.8 KB Download Demo Project - 77.6 KB
Introduction
ASP.NET 2.0 introduces a great new feature called Master Pages, which provides a framework for creating page templates that can be reused across a Web application. However, since ASP.NET 2.0 is currently in beta, much of the development community can not realize the benefits of Master Pages today. The goal of this article is to demonstrate a framework for creating page templates in ASP.NET 1.1, the Page Template Framework, which provides similar functionality to Master Pages and is organized in such a way to be easily migrated to The Master Pages Framework.
There are many articles floating around the Web that offer solutions for creating page templates in ASP.NET 1.1. I spent much time looking over these articles and discovered that there were many ways to accomplish the task. However, I had yet to find a solution That fit my needs.
One of the articles I had read, Peter Provost's ASP.NET Page Templates - Using Inheritance, prompted a proverbial "light bulb" to appear over my weary head I have worked with ColdFusion for many years, and one of the frameworks available for ColdFusion. development - Fusebox -. utilizes a "circuit" to organize the structure of components within a page This same idea could be merged with page inheritance to provide a dynamic and scalable solution for page templates in ASP.NET 1.1.
Configurable Page Templates
Almost every article I researched when considering this framework offered a solution that required a predefined page template be developed. This meant that any change to be made to the template would require the Web application to be recompiled and redeployed to the Web server. Although this may seem a trivial task in some cases, it's just a waste of time and effort. Considering this and other idiosyncrasies tied to developing templates, I decided to leverage a "configure vs. customize" approach.Changes to a page template should be configured rather than customized. If page inheritance is used as the templating mechanism, the base page must be modified and recompiled to incorporate changes. This customization makes maintaining dynamic page templates an unnecessarily tedious task. Using a configurable approach, changes to a page's template can be made in A Configuration File Which Defines a page template declarative.
The page template Framework
Based on this approach, i created the page template framework. Figure 1 Depicts The Main Components of the page template framework.
Figure 1 - the page template framework
As depicted in Figure 1, the Page Template Framework utilizes page inheritance to templates for use within a Web application. In order for a Web Form to utilize the Page Template Framework, it must inherit the PageBase class, which derives from the Page class. This .
The Page Template Framework defines page templates as a collection of User Controls to be added to a Page in a specific order, placed either before or after the content of the derived page. To do this, the Page Template Framework contains configuration components for declaratively defining page templates to be implemented by a Web application. The PageConfig class is an XML serialize-able class that represents the page template configuration for a Web application. This class is simply an API built to access the Page.config file, which is an XML File Containing The page template definitions for a Web Application.The Following IS A Sample Page.config File:
Listing A - Page.config
XML Version = "1.0" encoding = "UTF-8"?>
Controlplacement = "beforecontent" /> Controlplacement = "afterContent" /> Controls> PateTemplate> Controlplacement = "beforecontent" /> Controlplacement = "afterContent" /> Controlplacement = "afterContent" /> Controls> PageTemplate> PageTemplates> Page> Pageconfig> As outlined in Listing A, the Page.config file consists of two sections: Additionally, a page template can be specified as the default template for Pages that are not listed in the In order for a Page to implement the page template as defined in the Page.config file, it must derive from the PageBase class. During the OnInit method of every Page, controls are added to the Controls collection of that Page. The PageBase overrides the OnInit method of the Page class to implement the page template defined in the Page.config file. to implement the page template, the PageBase uses the PageConfig class to determine the controls that define the page template, and adds those controls to the Controls collection exposed Withnin the oninit method.listic b - pagebase.cs - Oninit Method /// /// Override the oninit method method of the page class, and ustement /// The controls listed in the point.config file /// summary> /// param> Protected Override Void OnNit (Eventargs E) { // Obtain the path to the point.config file for the capital application String pageconfigpath = server.mappath (pageConfig.pageconfigfilepath); // Check to see if the page.config file exists IF (file.exists (pageconfigpath)) { // Check to see reason the page.config file has been loaded Into cache IF (cache [pageconfig.pageconfigcachekey] == null) { // Create a cachedpendency on the point.config file CachedPendency Dependency = New System.Web.caching.cachedependency (pageConfigPath); // load the page.config file and insert the pageconfig inTo cache Cache.insert (PageConfig.PageConfigcacheKey, PageConfig.com (PageConfigPath), Dependency); } // load the page.config file into a pageconfig object PageConfig PC = (PageConfig) Cache [PageConfig.PageConfigcacheKey]; // Get a reference to the current page from the pageconfig Page Page = pc.findpage (request); // Locate The PageTemplate for The Current Page Object PageTemplate Template = (Page == NULL)? Pc.finddefaultTemplate () : pc.FindTemplate (page.templatename); // Check to Ensure a Valid Templatename Attribute Was Specified IF (Template! = null) { // Declare a Control Into Which a Template Will BE LOADED System.Web.ui.control control = null; // Iteerate Over the controls in the template to locate // the controls with beforecontent placement For (int idx = 0; idx { // Check to see if the capital control Has BeforeContent Placement IF ((Control) template.controls [idx]). Controlplacement == Controlplacement.beforecontent) { // Load the specified control from the path specified in the pageconfig Control = this.loadControl ((Control) template.controls [idx]). PATH); // Check to see if the control requires a unique name IF ((Control) template.controls [idx]). Uniqueename! = null) { // EnSure That the value is not an empty string IF ((Control) template.controls [idx]). Uniqueename! = String.empty { // set the idproperty of the control to the uniqueename attribute Control.id = (Control) template.controls [idx]). Uniqueename } } // add the contact to the current page This.Controls.addat (idx, control); } } // Initialize The Page Content Base.onit (e); // Iteerate Over the controls in the template to locate // the Controls with aftercontent placement Foreach (Control Ctrl in Template.Controls) { // check to see if the capital control Has AfterContent Placement IF (ctrl.controlplacement == controlplacement.afterContent) { // load the specified control from the path specified in the pageconfigControl = this.loadControl (ctrl.path); // Check to see if the control requires a unique name IF (ctrl.unique! = NULL) { // EnSure That the value is not an empty string IF (ctrinuniquename! = string.empty) { // set the idproperty of the control to the uniqueename attribute Control.id = ctrl.uniquename; } } // load the specified control and add it to the current page This.Controls.Add (Control); } } } Else { // Check to ensure That the page as listed in the pageconfig IF (Page! = NULL) { // the page has an invalid TemplateName Attribute, Throw an Exception Throw new Exception String.Format ("Invalid Templationename" "for Page /" {0} / ". there is no" "Template Named /" {1} / ".", Page.path, Page.TemplationName); } Else { // The page is not listed in the pageconfig, // So Initialize the page Content Base.onit (e); } } } Else { // Initialize The Page Content Base.onit (e); } } As detailed in Listing B, the PageBase class utilizes the PageConfig class to load the Page.config file into the Cache object. A CacheDependency is added to the Cache object based on the Page.config file path so that changes to the Page.config file will invalidate the value stored in the Cache object. Then, the PageBase class attempts to find the current Page (using the current HttpRequest instance) in the PageConfig. If the Page is found, its template is then located. If the Page has a valid TemplateName specified, the PageBase class will attempt to load the controls listed in the PageConfig in the order in which they are listed in the Page.config file. Their position relative to the content of the Page implementing the template is specified within each .. The sample application provided demonstrates the functionality of the Page Template Framework The only Page in the application, Default.aspx, is configured with content for Acme Business Corporation This Page initially implements the MainTemplate page template, which is listed below: Listing C - MAINTEMPLATE PAGE TEMPLATE Controlplacement = "beforecontent" /> Controlplacement = "beforecontent" /> Controlplacement = "beforecontent" Controlplacement = "beforecontent" Uniqueename = "generalnaVigation" /> Controlplacement = "beforecontent" /> Controlplacement = "afterContent" /> Controlplacement = "afterContent" /> Controls> PageTemplate> Note the additional attribute on the fourth Listing D - Programmatic Access to Controls // Find The General Navigation Control by it's uniqueename attribute PageTemplatesample.Controls.General.navigation NAV = (PageTemplatesample.Controls.General.naVigation) This.FindControl ("GeneralnaVigation"); To test this functionality, change the templatename attribute of the Listing E - CHANGING A Page's Template Not only will the content of Default.aspx appear within the Division page template, but the appearance of the Navigation.ascx User Control will change based on the programmatic access to the control, as outlined above. Considances This framework was developed to provide functionality for creating page templates for use within ASP.NET 1.1 Web applications; therefore, it is important to note that there are some considerations to make when implementing the framework In ASP.NET 2.0, Master Pages will provide. design-time support for the content of Master Pages The Page Template Framework does not provide design-time support for the content of the page templates;. however, you can still use the Web Forms designer to create the content of a Page utilizing the framework .The Page Template Framework, by virtue of its design, provides a system for defining page templates to be implemented in a class derived from the PageBase class. This design could be extended to enable declarative page template inheritance. Consider the following Page.config XML Fragment: Listing F - Page Template Inheritance Controlplacement = "beforecontent" /> Controlplacement = "afterContent" /> Controls> PageTemplate> Controlplacement = "beforecontent" /> Controlplacement = "afterContent" /> Controls> PageTemplate> As depicted in Listing F, declarative page template inheritance would allow nested page templates. Child page templates would be processed by the framework in between its parent's Controls placed before and after the derived Page's content. Another consideration for the framework is the notion of skinning. The Page Template Framework could be extended to provide personalized or localized versions of pages within a Web application. for example, the PageConfig class could be loaded into memory for users of the application. A user could then specify their skin preference within a form settings, Which Could The Serialize The Pageconfig Instance and Store IN A Database Or Session Store.conclusion Although it is likely that this framework will be rendered obsolete by the release of ASP.NET 2.0, it is still a good tool to use when developing Web applications that require a flexible user interface. It is also a good exercise for developers who are unfamiliar WITH Developing Template-based Applications, And Want To Get More Experience. Acknowledgements THANKS TO Sean Jones for Reviewing and Providing Input on this article.