grady
grady

Reputation: 12755

Loading css dynamically in ASP.NET

I am loading a css in my master page ...

<link rel="stylesheet" href="css/mystyles.css" title="styles" type="text/css" />

Now I want to load this dynamically according to a web.config key. Is there a better/ standard way of doing this, or is my idea the standard way?

Thanks

Upvotes: 3

Views: 4867

Answers (4)

ThisGuyKnowsCode
ThisGuyKnowsCode

Reputation: 531

Option 5:

Put your CSS in a new App_Themes subfolder, and use the web.config theme to set the theme name. Then load the theme from your master page's code behind. Be wary though; themes load CSS files alphabetically.

Upvotes: 0

Jamie Treworgy
Jamie Treworgy

Reputation: 24334

Option 4: Add the whole link to head in code

void AddStylesheet(string ssRef) {
    HtmlHead head = Page.Header;

    Literal l = new Literal(); 
    l.Text = "<link href=\""+ssRef + "\" type=\"text/css\" rel=\"stylesheet\" />";
    head.Controls.Add(l);
}   

... which is essentially similar to Option 2

Upvotes: 1

Tjaart
Tjaart

Reputation: 4119

Option 3:

In your head tag you can make the style sheet dynamic by storing the stylesheet path in a session variable:

 <link rel="stylesheet" type="text/css" href="<%=Session("PathToStyleSheet") %>" />

Upvotes: 0

Nikhil Vaghela
Nikhil Vaghela

Reputation: 930

Option 1:

You can add runat="server" attribute in your css link and set href value from code behind file where you can dynamically set it.

Option 2:

HtmlLink link = new HtmlLink();
link.Attributes["href"] = filename;
link.Attributes["type"] = "text/css";
link.Attributes["rel"] = "stylesheet";
Page.Header.Controls.Add(link);

Upvotes: 6

Related Questions