You are not Logged in
Would you like to Login or Register

Today is: 05 February 2012
Check this months hot topics

Dynamically adding a CSS link to the page in c#

The following code adds a CSS link to the header node of a Html page.

// register css
HtmlHead head = (HtmlHead)Page.Header;
HtmlLink link = new HtmlLink();
link.Attributes.Add("href", Page.ResolveClientUrl("~/App_Themes/Default/StyleSheet.css"));
link.Attributes.Add("type""text/css");
link.Attributes.Add("rel""stylesheet");
head.Controls.Add(link);
kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Paul Hayman Skype
Author : Paul Hayman
Published : 14 November 2007

Paul is the COO of kwiboo ltd consultant and has more than a decade of IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services. Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.

Comments

Randi said:

Thanks!! this fixed my issue!!

September 22, 2008 - 10:23 PM

Rahul Tyagi said:

Thanks it fixed my issue

Cheers!!!!!

December 30, 2008 - 7:08 AM

Manpreet said:

Can we use following one line code to link the css page in head tag?

<link href="Style.css" rel="stylesheet" type="text/css" />

this too works

May 08, 2009 - 6:20 AM

phayman said:

You can ad it in the Head tag, but in this example, we want to specify it dynamically.. different styles for different pages, browsers etc.

August 21, 2009 - 11:03 AM

IIPPP said:

LOL Manpreet you suck

September 24, 2009 - 6:50 AM

srinivas N said:

This code doesn't work when we have <% * %> tags under <head> tag.

In this case we will get a run time error mesage

"The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)."

December 30, 2009 - 1:37 PM

jj said:

hi i am using this concept in my application. Style sheet is dynamically changed. that's working fine . Is there any idea for my below question?

1. button click event i called one page . in that page i have used this dynamic style change functionality. First time the style is not changed after refresh only the style is changed

February 05, 2010 - 9:23 AM

Fernando said:

Thank you, solved my problem!

March 02, 2010 - 9:31 PM

shady said:

thanks alot man, this was really helpful....... best source i've found on this topic.

i really appreciate it

December 25, 2010 - 9:28 AM

figgi said:

hey guyz! iv really been having a problem with my code i get the error

Object reference not set to an instance of an object. at the line [head.Controls.Add(link); ]

who knows what am not doing?

January 19, 2011 - 3:42 PM

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views

More Publications

Select a row in an asp:GridView without using a Select Command
Paul Marshall - 14/06/2006
"This row already belongs to another table" Error
Paul Marshall - 24/10/2006
Authenticated HTTPRequests (Using Credentials)
Dave Howard - 04/09/2006
Creating a Tag Cloud in C#
Paul Hayman - 22/08/2006
Google Applicance - googleoff / googleon Tags
Paul Hayman - 27/09/2006