Click here to Skip to main content
13,355,018 members (56,116 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 3 Dec 2004

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

, 3 Dec 2004
Rate this:
Please Sign up or sign in to vote.
VB.NET line of code to dynamically set a link to a CSS file.


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

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).

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

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

Sub Page_Load(Sender As Object, E As EventArgs)
 If Not (IsPostBack)
 End If
End Sub

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


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
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...

You may also be interested in...


Comments and Discussions

GeneralAdding a CSS file dynamically in ASP.NET Pin
elizas10-Feb-10 0:02
groupelizas10-Feb-10 0:02 
GeneralA better way to add stylesheet to page header Pin
Eric Falsken12-Nov-07 14:36
memberEric Falsken12-Nov-07 14:36 
GeneralGreat! Pin
Ankhara24-Aug-07 1:09
memberAnkhara24-Aug-07 1:09 
GeneralThe closed '/' could go away Pin
Wayne G. L9-Dec-05 5:20
memberWayne G. L9-Dec-05 5:20 
QuestionCan not get to work Pin
Paul Hutchinson10-Aug-05 22:26
memberPaul Hutchinson10-Aug-05 22:26 
QuestionWhat object type is added as a result of this? Pin
Andy G Brown10-Jan-05 8:44
memberAndy G Brown10-Jan-05 8:44 
AnswerRe: What object type is added as a result of this? Pin
Paul Clapham21-Jul-05 11:06
sussPaul Clapham21-Jul-05 11:06 
AnswerRe: What object type is added as a result of this? Pin
jackrizzo4-Jan-06 6:35
memberjackrizzo4-Jan-06 6:35 
GeneralWrap in user control Pin
Steven Berkovitz7-Dec-04 14:38
memberSteven Berkovitz7-Dec-04 14:38 
QuestionPerformance? Pin
Kevin.Jensen4-Dec-04 8:12
sussKevin.Jensen4-Dec-04 8:12 

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
Web04 | 2.8.180111.1 | Last Updated 3 Dec 2004
Article Copyright 2004 by Florent Boulanger
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid