Page Templating for Web Applications
page 2 of 5
by J. Ambrose Little
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 30115/ 41

The Code

using System;

using System.Web.UI;
using System.Web.UI.WebControls;

namespace TemplateApp.Templates
{
/// <summary>
/// Base template for site
/// </summary>
public class PageTemplate : Page
{
// HTML title
private System.Web.UI.LiteralControl _title =
new System.Web.UI.LiteralControl();
// Stylesheet file location
private System.Web.UI.LiteralControl _styleSheet =
new System.Web.UI.LiteralControl();

/// <summary>
/// Common site web user control.
/// </summary>
protected TemplateApp.Components.TopBar TopBar;

/// <summary>
/// Temporary control collection to hold parsed controls.
/// </summary>
protected ControlCollection ControlBin;

/// <summary>
/// Placeholder for the HTML HEAD section.
/// </summary>
protected PlaceHolder Head = new PlaceHolder();
/// <summary>
/// Placeholder for the body/content section of the form;
/// all controls on inheritors will be added to this.
/// </summary>
protected PlaceHolder Body = new PlaceHolder();
/// <summary>
/// Standard form for the page
/// </summary>
protected System.Web.UI.HtmlControls.HtmlForm MainForm =
new System.Web.UI.HtmlControls.HtmlForm();

/// <summary>
/// Gets or sets HTML title for the page
/// </summary>
protected string Title
{
get
{
return this._title.Text;
}
set
{
this._title.Text = "My Site Title Prefix: " + value;
ViewState["PageTitle"] = this._title.Text;
}
}

/// <summary>
/// Gets or sets stylesheet location
/// </summary>
protected string StyleSheet
{
get
{
return this._styleSheet.Text;
}
set
{
this._styleSheet.Text = value;
ViewState["StyleSheet"] = this._styleSheet.Text;
}
}

/// <summary>
/// Receives the objects parsed from the ASPX page and
/// adds them to a private control collection.
/// </summary>
/// <param name="obj">Parsed object.</param>
protected override void AddParsedSubObject(Object obj)
{
// if this is the first time here,
// instantiate our control collection
if (ControlBin == null)
ControlBin = new ControlCollection(this);

// add the control to our holding bin collection
this.ControlBin.Add((System.Web.UI.Control)obj);
}

/// <summary>
/// Page init; calls private method LoadControls,
/// which adds IDs to template controls, loads the
/// TopBar control, and defines the template page layout
/// </summary>
/// <param name="e">Page init event arguments</param>
protected override void OnInit(EventArgs e)
{
this.LoadControls();
base.OnInit(e);
}

/// <summary>
/// Adds IDs to template controls, loads the TopBar control,
/// and defines the template page layout
/// </summary>
private void LoadControls()
{
// Load TopBar control; this is just an example of how to
// use a user control w/ the template; actual control
// not included in sample
this.TopBar = (TemplateApp.Components.TopBar)this.LoadControl(
this.Request.ApplicationPath + "/Components/TopBar.ascx");
// Add IDs to template controls
this.Head.ID = "HEAD";
this.TopBar.ID = "TopBar";
this.MainForm.ID = "MainForm";
this.Body.ID = "BODY";

// /////////////////
// Begin page layout
// /////////////////
// Add standard HTML, title, & stylesheet tags
AddStaticText(
"<!DOCTYPE HTML PUBLIC " +
"\"-//W3C//DTD HTML 4.0 Transitional//EN\" >" +
"<html>\n" +
"<head>\n" +
"<title>");
this.Controls.Add(this._title);
AddStaticText("</title>\n" +
"<link type=\"text/css\" rel=\"stylesheet\" href=\"");
this.Controls.Add(this._styleSheet);
AddStaticText("\">\n");
// Add Head to the HEAD section
this.Controls.Add(this.Head);
// Close Head section; open body section
AddStaticText(
"</head>\n" +
"<body>\n");
// Add the TopBar control to top of form
this.MainForm.Controls.Add(this.TopBar);
// Add a central div for body to form
this.MainForm.Controls.Add(
new LiteralControl("<div id=\"bodyDiv\">"));
// Load all of the controls from the child page into
// the Body PlaceHolder on the template
for(int i = 0; i < ControlBin.Count; i++)
{
switch (ControlBin[i].GetType().FullName)
{
case "System.Web.UI.WebControls.Image":
((Image)ControlBin[i]).ImageUrl =
this.Request.ApplicationPath + "/" +
((Image)ControlBin[i]).ImageUrl;
break;
case "System.Web.UI.WebControls.ImageButton":
((ImageButton)ControlBin[i]).ImageUrl =
this.Request.ApplicationPath + "/" +
((ImageButton)ControlBin[i]).ImageUrl;
break;
case "System.Web.UI.WebControls.HyperLink":
((HyperLink)ControlBin[i]).NavigateUrl =
this.Request.ApplicationPath + "/" +
((HyperLink)ControlBin[i]).NavigateUrl;
break;
}
this.Body.Controls.Add(ControlBin[i]);
}
// Add body placeholder to form
this.MainForm.Controls.Add(this.Body);
// Add close body div and add standard footer to form
this.MainForm.Controls.Add(new LiteralControl(
"</div>" +
"<div class=\"templateFooter\"><nobr>" +
"Copyright &copy; 2002 My Company</nobr></div>"));
// Add form to page
this.Controls.Add(this.MainForm);
// close standard body & HTML tags
AddStaticText(
"</body>\n" +
"</html>\n");
}

// Helper method to add literal controls to page
private void AddStaticText(string output)
{
this.Controls.Add(new LiteralControl(output));
}

/// <summary>
/// Loads view state for page.
/// </summary>
/// <param name="savedState">Saved state.</param>
protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
// Initialize Page Title & Stylesheet
this._title.Text = Convert.ToString(ViewState["PageTitle"]);
if (this._title.Text == string.Empty)
this._title.Text = "My Default Title Here";
this._styleSheet.Text = Convert.ToString(ViewState["StyleSheet"]);
if (this._styleSheet.Text == string.Empty)
this._styleSheet.Text = "myDefaultStyleSheet.css";
}


}


}



 


View Entire Article

User Comments

Title: An idea   
Name: Hans
Date: 2007-01-29 5:17:42 AM
Comment:
If you want intellisene without having to add body tags to the pages you can go into Tools -> Options -> Text Editor -> File Extension and add the "aspx" extension to the User Control Editor.

Cheers,
H
Title: Rendering Controls   
Name: Ambrose
Date: 2005-09-23 10:08:57 AM
Comment:
John, that sounds like a big question and one that isn't entirely clear (to me anyways). You might try asking over at the ASP.NET Forums; it's better suited to general questions.
Title: good article   
Name: John
Date: 2005-09-23 12:16:25 AM
Comment:
how do we render the controls to the web page and still be able to interact with the server as if it is a web server control.
Title: excellent   
Name: A Anderson
Date: 2005-09-20 10:16:29 PM
Comment:
Excellent work. I would like to express my interest in your part two. :-) thanks.
Title: Good Ideas!   
Name: Greg Olsen
Date: 2004-12-06 5:47:11 AM
Comment:
Got some great ideas/code from this on how to add user controls to build up a page using templates and inheritance.
Also see http://www.codeproject.com/aspnet/page_templates.asp
for more great info on page inheritance and asp.net templates. These two sites allow for any beginner to architect a good website structure.
Title: Very good   
Name: Yao Liu
Date: 2004-11-02 3:44:22 PM
Comment:
Helps me solve the view state problem
Title: Nicewhan bruvva!   
Name: Nik
Date: 2004-10-21 8:49:07 PM
Comment:
Good article. I'll find this very useful.






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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-19 1:23:34 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search