Loading css dynamically in ASP.NET

I upload css to my home page ...

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

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

thanks

+4
source share
4 answers

Option 1:

You can add the runat = "server" attribute to the css link and set the href value from the code behind the file in which 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); 
+6
source

Option 4: add the whole link to the head in the 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

+2
source

Option 3:

In the head tag, you can make a dynamic style sheet by saving the style sheet path in the session variable:

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

Option 5:

Put your CSS in the new App_Themes subfolder and use the web.config theme to set the theme name. Then download the theme from the main page code. Be careful; themes download CSS files in alphabetical order.

0
source

Source: https://habr.com/ru/post/1334511/


All Articles