ASP.NET - conditionally link CSS file

I want to conditionally associate a CSS file base with a user role. The administrator should link my admin.css file, while everyone else (other roles and anonymous users) should display my global.css file. It can be done?

+3
source share
5 answers

Try this :

protected void Page_Init(object sender, EventArgs e)
{
    HtmlLink css = new HtmlLink();
    // add conditional logic to add correct css file        
    css.Href = "css/fancyforms.css"; 
    css.Attributes["rel"] = "stylesheet";
    css.Attributes["type"] = "text/css";
    css.Attributes["media"] = "all";
    Page.Header.Controls.Add(css);
}
+6
source

Either you can create a theme based on the role - AdminTheme (will contain admin.css) and GlobalTheme (will contain global.css), or you can dynamically write the element <link>in the tag <head>after providing the runat = "Server" attribute.

PreInit Init .

0

, <head>, if , " ", <link> ( @import), , CSS).

0

, HtmlGenericControl , , .

0

/// , , " ", css , , , .

- . , Opera/Firefox, , , " ". , , css , .

0

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


All Articles