Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - Conditionally link CSS file

Tags:

css

asp.net

I want to conditionally link a CSS file base on the user's Membership role. An administrator should link my admin.css file while everybody else (other roles and anonymous users) should display my global.css file. Can this be done?

like image 874
Mike Cole Avatar asked Mar 01 '23 20:03

Mike Cole


1 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);
}
like image 134
Michael Haren Avatar answered Mar 05 '23 18:03

Michael Haren