Geeks With Blogs
Scott Van Vliet Once a Developer, Always a Developer

A few months ago, a colleague of mine approached me about page templates in ASP.NET.  I had done this before using page inheritance, so I referred him to a few links that I had collected that offered similar solutions.  He researched the articles, and decided to implement a solution for his client utilizing a page inheritance approach.

 

More recently, this topic had come up again, and I put some more thought into the approach that both my colleague and I had taken.  Although page inheritance provided a solid solution for creating page templates, it was not easy to manage the templates.  Any changes to be made to a page template would have to be made in the code, and the Web application would have to be recompiled in order for the changes to take effect.  So, I decided to develop a solution that streamlined this task.

 

The Page Template Framework for ASP.NET 1.1 builds upon page inheritance, and provides a configurable model for page template development.  Page templates are defined declaratively in an XML file called Page.config, which is deployed with a Web application.  Then, Pages can implement the templates by adding a reference to the Page in the XML file, and setting the TemplateName attribute to the appropriate template.  Moreover, a default page template can be defined so that any page that utilizes the framework (by deriving from the PageBase class) will apply that template.

 

Here’s a sample of what the Page.config file looks like:

 

<?xml version="1.0" encoding="utf-8"?>

<PageConfig>

  <PageTemplates>

    <PageTemplate Name="MainTemplate" IsDefault="true">

      <Controls>

        <Control Path="~/Controls/Header.ascx"

ControlPlacement="BeforeContent" />

        <Control Path="~/Controls/Navigation.ascx"

UniqueName="ctrlNavigation"

ControlPlacement="BeforeContent" />

        <Control Path="~/Controls/MainTemplate/Footer.ascx"

ControlPlacement="AfterContent" />

      </Controls>

    </PageTemplate>

    <PageTemplate Name="DivisionTemplate">

      <Controls>

        <Control Path="~/Controls/DivisionTemplate/Header.ascx"

ControlPlacement="BeforeContent" />

        <Control Path="~/Controls/DivisionTemplate/Footer.ascx"

ControlPlacement="AfterContent" />

      </Controls>

    </PageTemplate>

  </PageTemplates>

  <Pages>

    <Page Path="~/Default.aspx" TemplateName="DivisionTemplate" />

  </Pages>

</PageConfig>

 

Please check out my article at The Code Project for more information, source code and demo project.

 

http://www.codeproject.com/useritems/PageFramework.asp

 

Feedback on this is very welcome.  I am open to your thoughts and ideas!

 

Posted on Friday, September 10, 2004 3:29 AM ASP.NET , C# | Back to top


Comments on this post: Page Template Framework for ASP.NET 1.1

# re: Page Template Framework for ASP.NET 1.1
Requesting Gravatar...
This actually looks very nice. I've been using Page inheritance to do something similar... However you still have to actually inherit from each base page, and recompile if you change the location of the controls in your template.
<br>
<br>One thing that would make this really rad is to be able to specify different templates for entire directories:
<br>
<br>&lt;Page Path=&quot;~/Admin&quot; TemplateName=&quot;AdminTemplate&quot; /&gt;
<br>&lt;Page Path=&quot;~/Registered&quot; TemplateName=&quot;RegisteredTemplate&quot; /&gt;
<br>etc...
<br>
<br>Is this currently possible?
Left by Jeff Perrin on Sep 10, 2004 4:44 AM

# re: Page Template Framework for ASP.NET 1.1
Requesting Gravatar...
What about MasterPages (http://ASPAlliance.com/PaulWilson/Articles/?id=14) -- which is going to be the standard soon enough in ASP.NET v2.0 -- might as well use it now too!
Left by Paul Wilson on Sep 10, 2004 10:41 AM

# re: Page Template Framework for ASP.NET 1.1
Requesting Gravatar...
Thanks to everyone for their feedback!
<br>
<br>Jeff,
<br>
<br>This is a great idea! I will look into how this can be applied and post updates here.
<br>
<br>Aapo,
<br>
<br>I hadn't come across SiteMesh before. I took a look at the .NET implementation of SiteMesh, and it doesn't look like it is matured enough to be used (as the site lists that there are no release versions yet.)
<br>
<br>Paul,
<br>
<br>I had read your article on Wilson MasterPages, however the draw back that I saw in using this framework was that the pages templates have to be defined top-to-bottom within a control. Changes to the a MasterPage requires changes to the page template (the template control), whereas the approach I wanted was to provide a configurable solution (as you see in the framework I created, which provides a configurable XML file for maintaining page templates).
<br>
<br>Thanks again for all of your feedback!
<br>
<br>
<br>
Left by Scott Van Vliet on Sep 10, 2004 2:31 PM

# re: Page Template Framework for ASP.NET 1.1
Requesting Gravatar...
Jeff,
<br>
<br>I've updated the framework to allow for Regular Expressions to be specified for &lt;Page&gt; items.
<br>
<br>Thanks for the idea!
<br>
Left by Scott Van Vliet on Sep 14, 2004 4:43 AM

# re: Page Template Framework for ASP.NET 1.1
Requesting Gravatar...
Such brilliant idea. The steps are well done. - Paradise Home Improvement Charlotte
Left by Wallace Miller on Dec 16, 2016 10:23 AM

Your comment:
 (will show your gravatar)


Copyright © svanvliet | Powered by: GeeksWithBlogs.net