How do I dynamically add CSS file for ASP.NET ASPX page?

 Copyright © 2007-2012 www.AspDotNetFaq.com


To add CSS Stylesheeet file programatically to ASPX page we can use .NET HtmlLink class in the Page_Init event handler:
  protected void Page_Init(object sender, EventArgs e)
  {
    // Define an HtmlLink control.
    HtmlLink myHtmlLink = new HtmlLink();
    myHtmlLink.Href = "~/StyleSheet.css";
    myHtmlLink.Attributes.Add("rel", "stylesheet");
    myHtmlLink.Attributes.Add("type", "text/css");
    // Add the HtmlLink to the Head section of the page.
    Page.Header.Controls.Add(myHtmlLink);
  }

 Copyright © 2007-2012 www.AspDotNetFaq.com