Wednesday, November 05, 2008

ASP.Net - Dynamically Adding a CSS/StyleSheet Reference at run-time

Per Michael, I have removed a technique that was only available in Beta 1.

protected void Page_Init(object sender, EventArgs e)
{
   HtmlLink link = new HtmlLink();
   link.Href = "~/StyleSheet.css";
   link.Attributes.Add("rel", "stylesheet");
   link.Attributes.Add("type", "text/css");
   Page.Header.Controls.Add(link);
}

2 comments:

Rem said...

The blog is helpfull...
visit also asp.net example

Michael Freidgeim said...

LinkedStyleSheets was available only in Beta (see http://forums.asp.net/p/878854/901772.aspx#901772), so the first method is WRONG.