问题
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
回答1:
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);
回答2:
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
回答3:
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") %>" />
回答4:
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.
来源:https://stackoverflow.com/questions/4615944/loading-css-dynamically-in-asp-net