Thursday, June 04, 2009

Adding CSS Links On The Fly In C#

Here is an example of adding a link to a CSS dynamically on an ASP.Net page. I stole this from somewhere, but forgot where, so please forgive the lack of attribution.


  void AddSmallGridCSS ()
{
HtmlLink link = new HtmlLink ();
link.Href =
"~/App_Themes/" + Page.Theme + "/mystylesheet.css";
link.Attributes.Add (
"type", "text/css" );
link.Attributes.Add (
"rel", "stylesheet" );
Page.Header.Controls.Add ( link );

}


You don't have to use app themes, the file can be in any directory.


No comments: