Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Header - User Control

Status
Not open for further replies.

anonim1

Programmer
Dec 10, 2004
108
US
I am trying to dynamically "insert" a header.ascx file into all of my .aspx files. My header.ascx looks something like this:

Code:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="header.ascx.cs" Inherits="myNamespace.header" TargetSchema="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"[/URL] %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>My Title</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5">[/URL]
		<link id="cssLink" runat="server"></link>
	</HEAD>
		<body>
		<form id="Form1" method="post" runat="server">

I then have a Page.cs file which is a base class I created using instructions I found on the web:

Code:
using System;
using System.Web.UI;

namespace myNamespace
{
	public class Page : System.Web.UI.Page
	{
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			Controls.AddAt(0, LoadControl("header.ascx"));
		}
	}
}

Finally, my test.aspx page has the following code:

Code:
<%@ Page language="c#" Inherits="myNamespace.Page" %>
..... some HTML here .......
</form>
</body>
</HTML>

Logically, this should work the way it is written. Unfortunately, I am new to ASP.NET so I don't know what I'm doing wrong. I get the error "Unexpected end of file looking for </form> tag" in header.ascx. What seems to be happening is that it is trying to compile header.ascx before including it in my test.aspx. Since test.aspx has the closing tags for form, body, and HTML, it is not working.

Does anyone know what I can do to make this work? Eventually, I will have a footer file that will have all of the closing tags.. I assume that I can insert this into all of the pages by adding the following line to my Page class:

Code:
Controls.AddAt(1, LoadControl("footer.ascx"));

I am not sure if the integer 1 works there or if I need to do it another way. Please put forth your thoughts and comments. Thank you.

Varol
 
That header.ascx is not going to fly. None of the stuff that you have in there belongs in a UserControl. What's the purpose of breaking up an aspx and turning it into two ascx's? Ultimately what are you really trying to do?
 
I'm trying to avoid having to type in the title on every page. I'm also trying to avoid having to hardcode the stylesheet file on every page.

More importantly, if the title must change at some point, I don't want to have to go through each page and make the changes.

Also, I have written a small script to dynamically load a certain CSS file based on the user's browser. I don't want to have to put this script into the codebehind file of each .aspx page when (I think) I can put it in the codebehind file of the header.ascx and include it on all of the .aspx pages.

The article below is the reference I used to try to set up the dynamic header control:

If there is an easier/better way to do it, I would appreciate it if someone could share. Thanks.

Varol
 
This works for what you are doing.

YOUR ASPX PAGE:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="MyNameSpace.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<asp:placeHolder id="TitleHolder1" runat="server" />
<asp:placeHolder id="Styleholder1" runat="server" />
</HEAD>
<body >


YOUR CODE BEHIND:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TitleHolder1.Controls.AddAt(0, LoadControl("header.ascx"))
Styleholder1.Controls.AddAt(0, LoadControl("style.ascx"))
End Sub


YOUR STYLE CONTROL:

<%@ Control %>
<style>
.red { FONT-SIZE: 10px; COLOR: red }
.black { FONT-SIZE: 13px; COLOR: black }
</style>


YOUR TITLE CONTROL:

<%@ Control %>
<title>My New Title</title>



Keeping it Real, Simple!
 
If you are looking to be able to modify various parts of a page (such as the title) then I would suggest using Page Inheritance. A good example of this can be found at:


I use this to be able to modify attributes such as page title, javascript file, css file etc and it is very effective.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
That article rules! I got it to work without much trouble at all. Thank you ca8msm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top