Do more with your ASP.NET Page Template
 
Published: 21 Oct 2003
Unedited - Community Contributed
Abstract
Use ASP.NET Page Templates for both design consistency and functional reliability across developers for multi-designer pages.
by Sajid Rehman
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 31787/ 50

Introduction

When we hear "ASP.NET Page Templates", we usually think of it as a way to maintain design consistency on the website. But, ideally your page template should do more for you. Why stop with just design consistency? You should even try to get functional consistency out of it.

What is a Page Template?

When you have a team of ASP.NET developers building pages, it would be ideal if design & functional consistency were maintained in all of their pages. But, more often than not, this is not the case. At the end, you would have a set of pages - all looking & behaving differently from each other. This is where page templates come in – a page template is nothing but a class that would be responsible for rendering the features common to all pages like – header, logo, left/right menus, footers, help links, etc.

The Page Template class would inherit from System.Web.UI.Page. The code behind class for each page would then inherit from the Page Template class & not System.Web.UI.Page.

How do I create my page Template?

 

How do I create my page Template?

Create a class that inherits from System.Web.UI.Page.

Override the Render() method –

protected override void Render(HtmlTextWriter writer)

{

writer.Write(@"

<html>

<head></head>

<include html code for header, logo, left menu, tables (up to the point where your aspx form is to be shown)>

");

// this is responsible for the generation of you aspx

base.Render(writer);

 

writer.Write(@"

</td>

<close all tables. Include html code for footers, etc>

</body></html>

");

}

The above code is the simplest way to create a page template.

Now, make sure all your aspx pages have their code-behind inheriting from the class you have just created.

Also, be sure to

  • Remove all <HTML>, <HEAD>, <BODY>, <TABLE> start & end tags from your aspx source. Basically, remove anything that you have already included in the Render() method.
  • Remove all absolute positioning style attributes from your aspx source, i.e. code for a label that looked like

<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server">Label</asp:Label>

should now look like

<asp:Label id="Label1" runat="server">Label</asp:Label>

forums
What else can I do with my Page Template?

 

What else can I do with my Page Template?

We have seen how we can ensure design consistency by using Page Templates. Let’s go a bit further & try to do more with it.

We have to ensure that all ASP.NET developers, within a team, code their pages such that minimum rework is required during integration. This means that all pages have to be consistent in design & behavior.

Here are a list of behavioral features that would be common to all pages within a site –

  • Session handling – Normally, you would have to explicitly set common session variables like user ID, language, etc. Also, you would have to repeatedly do session checks to see if a valid user has logged in.
  • Internationalization – All pages would require access to resource files to handle the display of text in various languages. Also, you would have to explicitly set the Culture objects for each page.
  • Database connectivity – All pages would interact with a class that would handle database connections through ADO.NET. Or, your page itself would open a connection to the database.
  • Error handling – All pages would behave in the same manner when encountering an error – log the error, redirect to an error page & display the appropriate error message.
  • Use of utility classes All pages would interact with a set of utility classes for mailing, logging, etc.
  • Help function – All pages would behave in the same manner when the help button is clicked – open a help window, show help file. All help files would be grouped together.
  • Button functionality – All pages would have a common set of buttons. Some pages may have all buttons displayed & others may not. For example – a site may have 3 buttons in all their pages (Continue, Restart & Exit) except for their first & last page.

 Now, it is possible for you to provide all these features in your Page Template itself. You no longer have to worry about ASP.NET developers in your team providing these very features inconsistently across pages. Also, your developers would save time as they would not have to bother about these features.

How do I do it? Session handling

Session handling features can be provided in your Page Template by –

  • Overriding the OnLoad() method in your Page Template class.
  • In the OnLoad() method, do all session checks - e.g. check if user ID is available in the session.
  • If not, then redirect to an error page if the current page is not one of the three
    • The first page of your site
    • The page responsible for setting session variables.
    • The Error page

Advantages

  • Developers need not write repetitive code like validating the user’s session, redirecting incase of timeout/invalid user.
How do I do it? Internationalization

Internationalization features can be provided in your Page Template by –

  • In the OnLoad() method –
    • Check if the ResourceManager is in the Application object.
    • If not, create a new ResourceManager object pointing to your resource file & store it in the Application object.
    • Set the ResourceManager object in a private variable.
    • Also, set the CurrentCulture & CurrentUICulture objects based on the language variable in the session.
  • Provide a protected method in your Page Template (say GetResource()) that returns the appropriate text (from the resource file) for the key passed to it. This method uses the private ResourceManager object set in OnLoad().

Advantages

  • Developers do not have to repeatedly check the user’s language preference & then set the Culture objects. This is done by the OnLoad() method of the template.
  • A single method is used to get data from resource files.
  • The resource files are set in the Page Template class itself & not by the individual developer. This avoids any issues that may arise due to incorrect resource file names across pages & developers.
How do I do it? Database connectivity

Database connectivity & utility classes can be provided in your Page Template by having them set as properties. They are then accessible to your code-behind classes. Initialization of these properties can be done in the OnLoad(). Alternatively, you can provide methods to initialize them in your Page Template class. These methods can then be called from the code-behind.

Advantages

  • Ensures that all developers in the team use features like connection strings, utility classes, loggers, data access objects consistently.
  • Initialization of these utility classes can also be done in the template providing lesser room for inconsistency.
  • Since all developers have to access the utility classes via the template, it ensures that integration headaches are minimized.
  • Ensures that there can be no goof ups arising out of mismatches or incorrect connection strings, log file names, email ids, etc.
How do I do it? Error handling

Error handling features can be provided in your Page Template by –
  • Including an ErrorCode property in your PageTemplate class.
  • Add a method SetErrorCode(int code) that sets the above property & redirects to your error page. This method can be called from the code-behind when an error/exception is encountered.
  • Your error page would then be able to retrieve the error code by

int errorCode = ((PageTemplate)Context.Handler).ErrorCode;

This error code can be used to retrieve the appropriate error message from the resource file.

Advantages

  • Ensures that all developers in the team handle errors/exceptions consistently – thereby saving time during integration.
  • Ensures that redirection to an error page, error logging, & error display happen in exactly the same way across pages & developers.
How do I do it? Help function

Help features can be provided in your Page Template by –

  • Including a HelpURL property in your PageTemplate class. This property is used in the Render() method.
  • This property is set in the code-behind of your aspx.

Advantages

  • Ensures that developers in the team handle help features consistently – thereby saving time during integration.
  • No hard coding of paths for help files is possible by the developer. The paths have been hard coded in the template & as a result the developer has to just provide the name of the help file.
  • Setting up help in the template avoids problems arising out of some developers using absolute paths & others using relative paths to access the help files.
How do I do it? Button functionality

Button functionality can be provided in your Page Template by –

  • Including boolean properties in your PageTemplate class for each button. These properties are used in the Render() method to show/hide the respective button.
  • This property is set in the code-behind of your aspx.

Advantages

  • Developers need not bother about rendering of buttons on a page. Boolean properties control the display of buttons. This is most useful in wizard style pages.
  • Setting up buttons & their functionality in the template avoids problems arising out of some developers using absolute paths & others using relative paths.

You can also add more features to the template based on the needs of your site.

You can download a sample application by clicking here

Send your comments to sajidrehman@email.com



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 5:02:38 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search