Click here to Skip to main content
Click here to Skip to main content

Dynamically set a link to a CSS file in ASP.NET

By , 3 Dec 2004
 

Introduction

I had a tough time finding this online so I thought I would post it on CodeProject.com.

I wanted to be able to dynamically set a link to a CSS file. For example, for users to be able to personalize their site, change the look and feel.

Just add a runat attribute to your traditional link tag. ID attribute has to be present, and tag has to be properly closed (not like HTML).

<head>
<link id="MyStyleSheet" rel="stylesheet" type="text/css" runat="server" />
</head>

Then in your Page_Load, simply add a "href" attribute as below:

Sub Page_Load(Sender As Object, E As EventArgs)
 If Not (IsPostBack)
  MyStyleSheet.Attributes.Add("href","/css/flostyle.css")
 End If
End Sub

Nothing fancy, very simple, but very useful. It also works for meta tags.

License

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

Florent Boulanger
Web Developer
United States United States
Member
Broad engineering background with business knowledge (graduated from a top French engineering school training entrepreneurs), and excellent relationship skills that Flo utilizes in the I.T. field to streamline/improve company processes.
 
From web to embedded programming, from a big-5 consulting company to a local hospital, Flo shares his time between development and implementation lead.
Work with ASP.NET, VB.NET, LDAP, SQL Server, Oracle, MySQL...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAdding a CSS file dynamically in ASP.NET Pingroupelizas9 Feb '10 - 23:02 
GeneralA better way to add stylesheet to page header PinmemberEric Falsken12 Nov '07 - 13:36 
After reviewing a few of the different methods here and elsewhere on the web, I gave up and figured it out myself. I rather like this one better than the others because it dosn't require a placeholder on the page and can easily be added to a user control or control assembly:
 
void Page_Load(object sender, EventArgs e) {
Literal l = new Literal();
l.Text = "<link rel=\"stylesheet\" href=\"mystyle.css\" type=\"text/css\" />";
l.ID = "mystyle";
if(Page.Header.FindControl(l.ID) == null)
Page.Header.Controls.Add(l);
}

GeneralGreat! PinmemberAnkhara24 Aug '07 - 0:09 
GeneralThe closed '/' could go away PinmemberWayne G. L9 Dec '05 - 4:20 
QuestionCan not get to work PinmemberPaul Hutchinson10 Aug '05 - 21:26 
QuestionWhat object type is added as a result of this? PinmemberAndy G Brown10 Jan '05 - 7:44 
GeneralWrap in user control PinmemberSteven Berkovitz7 Dec '04 - 13:38 
QuestionPerformance? PinsussKevin.Jensen4 Dec '04 - 7:12 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 3 Dec 2004
Article Copyright 2004 by Florent Boulanger
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid