4

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

4 Answers 4

6

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Where would I write option 1? It seems the layout is misplaced ... in the page_load?
1

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

Comments

0

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") %>" />

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.