by J. Ambrose Little
Feedback
|
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days):
30397/
46
|
|
|
The Code |
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TemplateApp.Templates
{
public class PageTemplate : Page
{
private System.Web.UI.LiteralControl _title =
new System.Web.UI.LiteralControl();
private System.Web.UI.LiteralControl _styleSheet =
new System.Web.UI.LiteralControl();
protected TemplateApp.Components.TopBar TopBar;
protected ControlCollection ControlBin;
protected PlaceHolder Head = new PlaceHolder();
protected PlaceHolder Body = new PlaceHolder();
protected System.Web.UI.HtmlControls.HtmlForm MainForm =
new System.Web.UI.HtmlControls.HtmlForm();
protected string Title
{
get
{
return this._title.Text;
}
set
{
this._title.Text = "My Site Title Prefix: " + value;
ViewState["PageTitle"] = this._title.Text;
}
}
protected string StyleSheet
{
get
{
return this._styleSheet.Text;
}
set
{
this._styleSheet.Text = value;
ViewState["StyleSheet"] = this._styleSheet.Text;
}
}
protected override void AddParsedSubObject(Object obj)
{
if (ControlBin == null)
ControlBin = new ControlCollection(this);
this.ControlBin.Add((System.Web.UI.Control)obj);
}
protected override void OnInit(EventArgs e)
{
this.LoadControls();
base.OnInit(e);
}
private void LoadControls()
{
this.TopBar = (TemplateApp.Components.TopBar)this.LoadControl(
this.Request.ApplicationPath + "/Components/TopBar.ascx");
this.Head.ID = "HEAD";
this.TopBar.ID = "TopBar";
this.MainForm.ID = "MainForm";
this.Body.ID = "BODY";
AddStaticText(
"<!DOCTYPE HTML PUBLIC " +
"\"- "<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");
this.Controls.Add(this.Head);
AddStaticText(
"</head>\n" +
"<body>\n");
this.MainForm.Controls.Add(this.TopBar);
this.MainForm.Controls.Add(
new LiteralControl("<div id=\"bodyDiv\">"));
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]);
}
this.MainForm.Controls.Add(this.Body);
this.MainForm.Controls.Add(new LiteralControl(
"</div>" +
"<div class=\"templateFooter\"><nobr>" +
"Copyright © 2002 My Company</nobr></div>"));
this.Controls.Add(this.MainForm);
AddStaticText(
"</body>\n" +
"</html>\n");
}
private void AddStaticText(string output)
{
this.Controls.Add(new LiteralControl(output));
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
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";
}
}
}
|
|
|
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.
|
|
|
|