Web Design that taps into the haromny and vision of your dreams.

Adding StyleSheets to ASP.Net via C#

How to add stylesheets to ASP.Net web pages from C#

Written By on in C#

305 words, estimated reading time 2 minutes.

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? In this tutorial we will see how to manually add HtmlLinks using C# code behind.

Active Server Pages (ASP.Net) Series
  1. What is ASP.Net?
  2. ASP.Net for PHP Developers
  3. Creating a Simple ASP.Net Page
  4. ASP.Net Website Navigation Using a SiteMap
  5. Using ASP.Net Master Pages and Content Pages
  6. Validating Input using ASP.Net
  7. Tracing and Remote Debug in ASP.Net
  8. Creating Custom Error Pages with ASP.Net
  9. Managing Session State in ASP.Net
  10. Using Themes and Skins in ASP.Net
  11. Creating User Controls in ASP.Net
  12. Difference between ASP.Net User and Custom Server Controls
  13. Creating Fully Themable Websites with ASP.Net
  14. Ultimate Guide to the Web.Config File
  15. Adding StyleSheets to ASP.Net via C#
  16. Extending the Web Sitemap Xml Document

In this short tutorial I am going to show you how to use the HtmlLink class to add a stylesheet to the header of an ASP.Net masterpage. You can also use this in the same way for a standard aspx page.

The first thing to do is create a HtmlLink object.

HtmlLink styleLink = new HtmlLink();

Next I'll add two attributes to the link:

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

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

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.

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:

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

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:

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

Last updated on: Wednesday 21st June 2017

 

Comments

There are no comments for this post. Be the first!

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me