Dynamically adding stylesheets and meta data to ASP.NET pages

Adding stylesheets to a page at runtime couldn’t be easier.

Here we load a different stylesheet based on the browser name (needless to say this could be extended to cover different versions/OSs etc.)

HtmlLink link = new HtmlLink();
link.Attributes.Add(“type”, “text/css”);
link.Attributes.Add(“rel”, “stylesheet”);

switch (Request.Browser.Browser)
{
case “Firefox”:
link.Attributes.Add(“href”, “cssFF.css”);
break;
case “IE”:
link.Attributes.Add(“href”, “cssIE.css”);
break;
}
this.Header.Controls.Add(link);

Advertisements

2 thoughts on “Dynamically adding stylesheets and meta data to ASP.NET pages

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s