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!