Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
3 Dec 2004 1  
VB.NET line of code to dynamically set a link to a CSS file.

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