Dynamically Adding StyleSheets to ASP.Net via C#

In this short tutorial, I am going to show you how to use the HtmlLink class to dynamically add StyleSheets to ASP.Net master pages.

By Tim TrottC# ASP.Net MVC • January 22, 2010
Dynamically Adding StyleSheets to ASP.Net via C#

In some situations, you may want to add link information to the HTML header, for example, stylesheets or RSS feeds. This can be done by hard coding into the .aspx file but what about dynamically adding links?

This tutorial will show you how to manually add HtmlLinks using C# code behind. You can also use this in the same way for a standard aspx page.

The first thing to do is to create a HtmlLink object.

C#
HtmlLink styleLink = new HtmlLink();

Next, I'll add two attributes to the link:

C#
styleLink.Attributes.Add("rel", "stylesheet");
styleLink.Attributes.Add("type", "text/css");

Next, I'll set the location of the stylesheet to load:

C#
styleLink.Href = "http://mydomain.com/css/mystylesheet.css";

The final step is to add this styleLink to the header. I usually do this in the Page_Load event.

C#
protected void Page_Load(object sender, EventArgs e)
{
  HtmlLink styleLink = new HtmlLink();
  styleLink.Attributes.Add("rel", "stylesheet");
  styleLink.Attributes.Add("type", "text/css");
  
  // Get the stylesheet from DB or build up string
  styleLink.Href = "http://mydomain.com/css/mystylesheet.css"; 
  
  this.Page.Header.Controls.Add(styleLink);
}

When executed this code will send the following HTML to the browser:

xml
<link rel="stylesheet" type="text/css" href="http://mydomain.com/css/mystylesheet.css" />

Meta Tags

You can use a very similar technique to add meta information to the header, for example, keywords and description. Instead of using the HtmlLink class, use a HtmlMeta class.

C#
HtmlMeta meta = new HtmlMeta();
meta.Attributes.Add("name", "keywords");
meta.Attributes.Add("content", "these, are, my, html, meta, keywords");
Page.Header.Controls.Add(meta);

When executed this code will send the following HTML to the browser:

xml
<meta name="keywords" content="these, are, my, html, meta, keywords" />

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.