日期:2012-08-27  浏览次数:20388 次

ASP.NET Page Templates
By Mike Borromeo
What?
A website templating system is a small collection of page templates that contain and control any non page specific (redundant) code and HTML.
Who?
This applies to anyone looking for a flexible method for implementing page templates in ASP.NET. Readers new to templates will find them invaluable. Readers trained in the template arts but looking for a better implementation already know the value of templates and hopefully will find the methods described here useful for their specific situation (if not I’d like to hear from you, if so I’d also like to hear from you).
Why?
In a one page website page templates would be of little use, but when you start building 5+ page sites life starts to get difficult sans templates. A well designed templating system (and I emphasize well designed) has the following benefits to name a few: more efficient and easier development through the elimination of redundant blocks of code, easier integration of designers and programmer through separation of HTML and server-side script, and easier development of consistent user interface through forced reuse of UI code.
Where & When?
Me personally, I would use templates in any site that has more than one page. And really there’s no reason not to, so I suppose these ideas apply everywhere and all the time.
How?
Ah How… the fun part. Here we’ll take a look at an example of a template and a page that employs the template. The entire collection of files you will need along with a more advanced example is available for download.
So you ask, is it difficult to understand or implement? In a word, no. In two words, heck no. If you can count to ten you can use templates. Ok, maybe that’s a bit of an overstatement but nevertheless. You’ll be templating, as it were, in no time. First let’s take a look at a simple example of a page that uses a template (a.k.a. a templated page).
<%@ Page Language="c#" Codebehind="Default.aspx.cs" Inherits="Example._Default" %><%@ Register TagPrefix="example" TagName="template" Src="/Includes/Template.ascx"%><example:template title=”Example of Page Templating” id="template" runat="server" >  <body>    Here lies the page content.  </body></example:template>
This is about as simple as a templated page gets. You should have a vague idea what everything here means but I’ll explain everything starting at the top. The Page directive isn’t specific to this project so we won’t detail it’s meaning. The register directive simply tells ASP.NET that we’re going to be using the template located as /Includes/Template.ascx. What’s that you say? User control? Why yes it is. The thinking in making a template a user control is this: you write C# in a C# editor and HTML in an HTML editor. Obvious? Maybe. But I’ve seen templates proposed that are implemented as a custom control (written in a code editor, not an HTML editor). It’s just easier to write HTML in the VS .NET HTML editor just as it’s easier to write C# in the C# .NET editor and with user controls we’re allowed this luxury. Intellisense anyone? You could say sure, you can design the template in the HTML editor the convert it to C# rendered output but then enters the question of maintainability. No designer I know wants to go in an edit a C# file and recompile the bloody web application. Ok, now I’m hearing something about speed concerns? Well granted compiled code custom control code is faster than the equivalent user control code but it is said that caching erases all your sins or was it cache is king, or maybe cache and thou shall be saved. Whichever the case, by using simple native caching techniques in ASP.NET you can eliminate many of the slowdowns caused by dynamic pages. H