Click here to Skip to main content
13,298,451 members (43,892 online)
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 27 Oct 2005

A Simple Way to Implement Page Header Data in ASP.NET

, 27 Oct 2005
Rate this:
Please Sign up or sign in to vote.
A class that makes it easy to implement page header tags in ASP.NET.


ASP.NET HeadFilter class

The ability to dynamically set the values of various tags that can appear in the HEAD section of a web page is often needed. One of the shortcomings of the ASP.NET model is that the dynamic content must have the "runat=Server" attribute, and tags with this attribute must appear within the server form tag. This makes it a non-trivial task to set these values via code.

This article describes a solution to this problem. The existing solutions that I found either required me to do things that Visual Studio didn’t like, or that I didn’t like. This solution is very simple to set up (can be used with one line of code) and does not interfere with your existing setup at all. You don’t have to change your page inheritance hierarchy, and you don’t have to do things that Visual Studio will complain about either.

To use this in your site, simply add a reference to the DLL (if you've first compiled it), or insert the .vb class file directly into your project, and use the class as needed.


The most simple usage

In the page Load event, add one line of code:

Response.Filter = _
   New HeadFilter(Response.Filter, "<title>My Title Here</title>")

You are simply passing in the exact text that you want to have between the <head> and </head> tags. In this case, a title for the page. If you want to pre-build a huge string that includes not only the title, but CSS links, JavaScript links, META tags, etc., that’s all you have to do.

More granular usage

Of course, you can use the class in a more granular way if you prefer. Here is an example of that usage. Again, in the page Load event, add the following code:

Dim myFilter As New HeadFilter(Response.Filter)
With myFilter
   .Title = "My Page Title"
   .BaseTarget = "_new"
   .AddMetaTag("Author", "", "Travis", "")
End With
Response.Filter = myFilter

So, what’s a filter, anyway?

Basically, ASP.NET gives you a way to look into (and modify) the bytes just before they go to the browser. The Response.Filter class is a Stream object representing the data that is going to the browser. There are many articles online explaining the use of filters for various purposes. The idea is that you can write your own filter, and apply it into the pipeline.

My filter simply looks for the HEAD tags, and puts the text between them, without modifying the rest of the document.

So, what can I put in there?

Just about anything. But the browser (and in some cases the server or the router) is expecting certain things, like the page title, links to style sheets, etc. Here is a summary:

The BASE tag example

<BASE target="_blank">
<BASE href="">

These are implemented via the .BaseTarget and .BaseHREF properties.

The STYLE tag example

<style type="text/css">
body {background-color: red}
p {margin-left: 20px}

This is implemented via the .Style property.

The TITLE tag example

<title>My Page Title</title>

This is implemented via the .Title property.

The LINK tag example

<link rel=stylesheet type=text/css href=/framework/styles_DEV.css>

This is implemented via the .AddLinkTag method. Add as many as you need.

There are two overloads of this method, and one derived method. This is because the <link> tag has eight attributes, but only three are used most commonly. I gave an overload which only wants the three attributes most often used, and another so that you could use all the eight if need be. If you are using a link tag to add a style sheet, I’ve added a derived method:

  • .AddStyleSheetReference method - Just pass in the path to the style sheet and it will create an appropriate link tag.

The META tag example

<meta name="Author" content="Travis Laborde">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">

These are implemented via the .AddMetaTag method. Add as many as you need.

The method lets you pass in both the Name and the HTTP-Equiv attributes. It should be noted that HTML guidelines stipulate that you should only use either the Name or the HTTP-Equiv in any given Meta tag. If you pass in the Name, it will be used, and the HTTP-Equiv will be ignored. Otherwise, HTTP-Equiv will be used.

The SCRIPT tag example

Because the Script tags are a little more complex, no specific property or method has been created to handle them. To insert script tags into your header, use the following general-purpose method instead:

  • .AddLiteralText method

    Use this method to add script blocks, or anything else you need that is not otherwise available using this class.

Things to consider

  • If you are using other filters on your response stream, some tweaking may be needed to ensure that this doesn't interfere with that.
  • The filter currently replaces any text between the <head> and </head> tags. If you are also using other means to get data there, this will overwrite it. For example, Visual Studio puts a few meta tags in by default, including a page title equal to the form name. This filter replaces those with whatever you create via your code.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Generalformatting of head and title tags of 2.0 Pin
Member 455086316-Jan-09 23:21
memberMember 455086316-Jan-09 23:21 
QuestionAdd attributes to body tag? Pin
wegnwa6-Mar-06 13:00
memberwegnwa6-Mar-06 13:00 
GeneralCode Adapted to C# Pin
Ricardo Casquete21-Feb-06 22:37
memberRicardo Casquete21-Feb-06 22:37 
GeneralAppend HEAD tags Pin
Death Angel9-Nov-05 5:57
memberDeath Angel9-Nov-05 5:57 
GeneralRe: Append HEAD tags Pin
travislaborde9-Nov-05 6:09
membertravislaborde9-Nov-05 6:09 
GeneralRe: Append HEAD tags Pin
Death Angel9-Nov-05 6:43
memberDeath Angel9-Nov-05 6:43 
GeneralHead tag in ASP.NET 2.0 Pin
jjrdk29-Oct-05 4:31
memberjjrdk29-Oct-05 4:31 
GeneralRe: Head tag in ASP.NET 2.0 Pin
Ricardo Casquete21-Feb-06 6:49
memberRicardo Casquete21-Feb-06 6:49 
GeneralThat's cool Pin
pbrooks27-Oct-05 9:11
memberpbrooks27-Oct-05 9:11 
GeneralI'm not sure you're right about... Pin
Mike Ellison27-Oct-05 7:28
memberMike Ellison27-Oct-05 7:28 
GeneralRe: I'm not sure you're right about... Pin
travislaborde27-Oct-05 7:45
membertravislaborde27-Oct-05 7:45 
GeneralRe: I'm not sure you're right about... Pin
Mike Ellison27-Oct-05 8:40
memberMike Ellison27-Oct-05 8:40 
GeneralRe: I'm not sure you're right about... Pin
worshiprick31-Oct-05 8:52
memberworshiprick31-Oct-05 8:52 
GeneralRe: I'm not sure you're right about... Pin
Mike Ellison31-Oct-05 8:55
memberMike Ellison31-Oct-05 8:55 
GeneralRe: I'm not sure you're right about... Pin
Brian Leach31-Oct-05 14:18
memberBrian Leach31-Oct-05 14:18 
GeneralHtmlGenericControl Pin
Brian Lowe1-Nov-05 12:44
memberBrian Lowe1-Nov-05 12:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 27 Oct 2005
Article Copyright 2005 by travislaborde
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid