in

ASP.NET Weblogs

Phil Scott's WebLog

Quite exciting this computer magic

Accessing the Html Header in ASP.NET 2.0

I feel a bit silly for not figuring this out quicker, so hopefully I can redem myself by posting this to help people out.  Taking a look at the Page class there is a Header property that looks tempting to be able to do something like dynamically add a stylesheet.  The problem is that when you type in Page.Header it doesn't appear that you have full control over the header, even though there is the ever so tempting System.Web.UI.HtmlControls.HtmlHead class.  What I had been doing is throwing an id and a runat="server" onto the head tag in my HTML, which I really didn't like because an id tag on the head tag isn't valid XHTML 1.1. 

Today, a break through.  This has always bothered me, so I took a deeper look and it turns out that Page.Header is defined as IPageHeader.  Perhaps, just maybe I could cast Page.Header into System.Web.UI.HtmlControls.HtmlHead.  And sure enough, it worked great.  Anyways, I feel a little silly about not figuring that out sooner but now that I've gotten this working I feel much better.  Anyways, here's some example code to add a stylesheet to a page:

        Dim header As Web.UI.HtmlControls.HtmlHead
        header = TryCast(Me.Page.Header, Web.UI.HtmlControls.HtmlHead)
        If header IsNot Nothing Then
            Dim link As New HtmlLink
            link.Attributes.Add("href", "~/whatever.css")
            link.Attributes.Add("media", "screen")
            link.Attributes.Add("rel", "stylesheet")
            link.Attributes.Add("type", "text/css")
            header.Controls.Add(link)
        End If

Comments

 

Michal Chaniewski said:

Wow, what a nice trick. I too didn't find that before. I will definitely have an use for that! :)
August 30, 2005 6:31 PM
 

Keyvan Nayyeri said:

August 31, 2005 1:19 AM
 

yamum said:

so ohw exactly can i do this can u plz send me the instructions step by step
September 8, 2005 2:31 AM
 

Phil Scott said:

Um, step by step instructions would be:
1) take code I posted and paste it in page_load or something
2) finished.
September 8, 2005 10:31 AM
 

Keyvan Nayyeri : Accessing HTML header in ASP.NET 2.0 said:

July 14, 2006 1:33 PM
 

Keyvan Nayyeri said:

Phil Scott has described how to access the HTML header in ASP.NET 2.0 One of enhancments in this new

July 16, 2006 1:34 AM
 

Rob siklos said:

If you want to do it with a tag which does not have a .NET implementation, you can use a generic object:

Dim script As New HtmlGenericControl("script")

script.Attributes.Add("type", "text/javascript")

script.Attributes.Add("src", urlbase & "js/validate.js")

Me.Header.Controls.Add(script)

May 29, 2007 8:26 PM
 

Willem said:

If you want to generate CSS on the fly and add it as embedded <style> in the page output HTML you can try this (C#):

string myCss = "body{ background-color: black; }"

string cssStyle = "<style type=\"text/css\">" + myCss + "</style>";

Literal cssLiteral = new Literal();

cssLiteral.Text = cssStyle;

Page.Header.Controls.Add(cssLiteral);

August 12, 2007 5:22 PM
 

dhruv said:

I want to add Templates in My WebSites. But I got Comfused after lot muh try.

September 17, 2007 5:59 AM
 

Cedric Valiente said:

Thank a lot for that post, I was using the runat="server" thing in the header and I never liked it.

@Rob siklos

A method to add dynamically an include to a javascript file exists.

Use that in the Page_Load :

Page.ClientScript.RegisterClientScriptInclude( "aKeyToIdentifyIt", "./Script/MyScriptFile.js");

It's bit easier ;)

September 27, 2007 6:18 AM
 

Steve said:

What am I missing here?  Don't you still have to put a runat="server" statement in the <head> tag?  I get no instance of Page.Header without a runat="server" statement.  Therefore, the statements in your If/Then loop never execute.

November 9, 2007 1:49 PM
 

George said:

It's the fact you don't have to put an ID in, the runat="server" has to stay.

February 27, 2008 11:53 AM
 

fayaz said:

hi please replay heade manu how to call other page in the forms

March 4, 2008 9:18 AM
 

Moms work from home. said:

Work from home moms. Top work at home moms. Voyforums work at home moms. Moms work at home. Work at home moms message boards.

October 30, 2008 6:23 PM
 

Easwaran said:

Really fruitful trick. This makes my life easy :-)

December 5, 2008 2:00 AM
 

l2 said:

wow .. this is great!!!

December 16, 2008 12:28 PM
 

English Bloke said:

"but now that I've gotten this working"

gotten is such a horrible word, makes you sound like a moron. If you need to flesh out this phrase then why not "but now I have this working"...

January 15, 2009 5:59 AM
 

Adam.Kahtava.com / AdamDotCom.com » The Problems with Themes, Skins, and Cascading Style Sheets (CSS) in ASP.NET 2.0 _ Defining a Media Type(s) (Work Around #2) said:

Pingback from  Adam.Kahtava.com / AdamDotCom.com &raquo; The Problems with Themes, Skins, and Cascading Style Sheets (CSS) in ASP.NET 2.0 _ Defining a Media Type(s) (Work Around #2)

May 5, 2009 8:06 AM
 

Cjmos said:

Or in a real language ;)

       HtmlHead header = this.Page.Header as HtmlHead;

       if (header != null)

       {

           HtmlLink link = new HtmlLink();

           link.Attributes.Add("href", "~/whatever.css");

           link.Attributes.Add("media", "screen");

           link.Attributes.Add("rel", "stylesheet");

           link.Attributes.Add("type", "text/css");

           header.Controls.Add(link);

       }

June 10, 2009 10:48 AM
 

Purav Thakkar said:

hello,

HtmlLink lnkHeade = new HtmlLink();

           lnkHeade.Attributes.Add("rel", "shortcut icon");

           lnkHeade.Attributes.Add("href", "../Images/Favorites/Classic car yellow.ico");

           Page.Header.Controls.Add(lnkHeade);

I have used this, but its not working in IE8, please help me out

July 28, 2009 9:53 AM
 

Jimbo said:

Wow thanks! This really did help, thankyou so much! This is great if you are creating an application with themes such as myself, so you can dynamically insert css.

August 20, 2009 3:34 PM
 

Raju Singh said:

i

want to link my page content at a link bar

September 18, 2009 7:33 AM

Leave a Comment

(required)  
(optional)
(required)  
Add