ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

AddCSS

Dynamically adds a cascading style sheet (a.k.a. CSS) file to a page even if using master page.

Source

public static void AddCSS(this Page page, string url)
{
    HtmlLink link = new HtmlLink();
    link.Href = url;
    link.Attributes["rel"] = "stylesheet";
    link.Attributes["type"] = "text/css";
    page.Header.Controls.Add(link);
}

Example

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.AddCSS("cssFile.css");
    }
}

Author: Paulo Castilho

Submitted on: 1 jun. 2010

Language: C#

Type: System.Web.UI.Page

Views: 5602