Menu
Home
About Me
Articles
Hobbies
Code Library
Flash Library
3d stuido max

 

Have you ever wanted to easily use the old asp of includes to create templated pages in .Net/C#.

Something like;
<!--#include file="header.asp"-->
<form><Do Stuff</form>
<!--#include file="footer.asp"-->

There are several ways to handle this, the most common is UserControls, but I found them lacking in certain functionality. So I figured out this little trick. To simplifiy things, I am only going to pass a value to change the page title.

There are three parts to this. The HTML code on the control, the HTML code on the base page, and the code behind on the base page. Lets pretend the control page is named header.ascx, and the base page is default.aspx

On the ASCX form, header.ascx

<head>
<title runat="server" id="PageTitle" />
</head>

On your new page,default.aspx, register the control and create an object with the new tag
<%@ Register TagPrefix="header" TagName="HeaderText" Src="header.ascx" %>
<header:HeaderText runat="server" ID="header" NAME="header"/>
		<form method="post" runat="server">
		</form>
	</body>
And lastly, on the codebehind, default.cs, connect everything together.
	
protected header header; //control name 

private void Page_Load(object sender, System.EventArgs e)
{
	HtmlGenericControl PageTitle  = (HtmlGenericControl)header.FindControl("PageTitle");
	PageTitle.InnerText = "Welcome to ItSuckToBeJoe.com";
}
Bingo, instead templated pages with values within the header file control being sent from the base page.