![]() |
Web Development »
ASP.NET »
General
Intermediate
Do more with your ASP.NET Page TemplateBy Sajid RehmanPage templates can provide more than just design consistency |
Windows, .NET 1.0, ASP.NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
When you have a team of ASP.NET developers building pages, it would be ideal if design and 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.
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
<HTML>, <HEAD>, <BODY>, <TABLE> start &
end tags from your aspx source. Basically, remove anything that you have
already included in the Render() method.<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>
We have seen how to 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 �
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.
Session handling features can be provided in your Page Template by �
OnLoad method in your Page Template class.OnLoad method, do all session checks - e.g. check if user ID is available in the session. Advantages
Internationalization features can be provided in your Page Template by �
OnLoad method � ResourceManager is in the Application object. ResourceManager object pointing to your resource file
& store it in the Application object. ResourceManager object in a private variable. CurrentCulture and CurrentUICulture objects based on the
language variable in the session.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
OnLoad method of the
template.
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
Error handling features can be provided in your Page Template by �
ErrorCode property in your PageTemplate class.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.int errorCode = ((PageTemplate)Context.Handler).ErrorCode;
This error code can be used to retrieve the appropriate error message from the resource file.
Advantages
Help features can be provided in your Page Template by �
HelpURL property in your PageTemplate class. This property is used in
the Render() method.Advantages
Button functionality can be provided in your Page Template by �
boolean properties in your PageTemplate class for each button. These
properties are used in the Render() method to show/hide the respective
button.Advantages
You can also add more features to the template based on the needs of your site.
The sample ASP.NET application provided is an example of the page template being put to use. The page template class is PageTemplate.cs. All code-behind classes inherit from it. The example includes a form (FirstPage.aspx) that submits to a details page (SecondPage.aspx). Also provided are a search page (searcher.aspx) & an error page (Error.aspx).
Take a look at each of these pages & note how almost no code (per aspx) is required to provide session checks, i18n setup, DB & utility class setups, error handling, etc. Yet, every page behaves in exactly the same manner & at all times ! All the code providing the above listed features are in PageTemplate.cs.
If you were using a normal page template then every developer would have to include the same code in every page - to provide all the features listed above. We all know how painful that can be !
Here's a quick guide on how to use this template in a new project -
OnLoad() method of PageTemplate.cs, i.e the following line in OnLoad()Application["resources"]
= new ResourceManager("MyTemplate.MyResources",Assembly.GetExecutingAssembly());
now becomes
Application["resources"]
= new ResourceManager("TestTemplate.MyResources",Assembly.GetExecutingAssembly());
Rebuild the project.<%@ Page language="c#" Codebehind="FirstPage.aspx.cs" AutoEventWireup="false"
Inherits="TestTemplate.FirstPage" %>
<form id="FirstPage" method="post" runat="server">
</form>
Add controls to the form in the designer mode. Do not add buttons to submit the form.System.Web.UI.Page to PageTemplate.Page_Load method, do the following
if ( IsPostBack )
{
// do any action on postback
}
else
{
// get Language code & user ID from previous HTML page or elsewhere
string languageCode = "en";
string userID = "superman";
// this call sets the language & user ID in the session
setLanguageAndUser(languageCode, userID);
}
// This sets the help file's url for this page
HelpURL = "first_page_help.html";
continueForm(). This method should be overridden in the HTML source of every aspx to submit the form. i.e. - you can include the following in the HTML source of the aspx page
<script>
function continueForm() {
alert("submitting......");
document.FirstPage.submit();
}
</script>
Now, see what happens when you click on Continue. Please note that you can provide alternate means to submit a form. I happened to use the Continue hyperlink.Page_Load & see what happens. i.e. in the postback section -
if ( IsPostBack )
{
try
{
throw new Exception();
}
catch (Exception ex)
{
SetErrorCode(1);
}
}
You will see that on submitting, the page automatically gets redirected to the Error page. Also, a message picked up from the resource file is shown.continueForm(), if necessary.setLanguageAndUser() in any page except for the first aspx page (i.e. FirstPage.aspx here).Render method of PageTemplate.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Dec 2002 Editor: Chris Maunder |
Copyright 2002 by Sajid Rehman Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |