tags:

views:

36

answers:

2

Hi all,

I have a page in my application where i need to load two css files dynamically based on the user who is logging in.

How can I load two css files in a single page dynamically?

This is my code to generate the css dynamically:

This is my code:

HtmlLink objCSS = new HtmlLink();

  objCSS.Attributes.Add("href", "Includes/css/ADxMenuEmbed.css");

  objCSS.Attributes.Add("rel", "stylesheet");

  objCSS.Attributes.Add("type", "text/css");

   Header.Controls.Add(objCSS);

Can anyone provide some ideas or samples to solve this problem?

+3  A: 

Following is way you can add css file dynamically in you asp.net application

if you want to load file as per user login you have to put if condition on role of use or something that is unique you are using

protected void Page_Init(object sender, EventArgs e)
{

  HtmlLink css = new HtmlLink();
  css.Href = "css/fancyforms.css";
  css.Attributes["rel"] = "stylesheet";
  css.Attributes["type"] = "text/css";
  css.Attributes["media"] = "all";
  Page.Header.Controls.Add(css);
}
Pranay Rana
i am using the same code but only one css is applied and not the other one This is my code: HtmlLink objCSS = new HtmlLink(); objCSS.Attributes.Add("href", "Includes/css/ADxMenuEmbed.css"); objCSS.Attributes.Add("rel", "stylesheet"); objCSS.Attributes.Add("type", "text/css"); Header.Controls.Add(objCSS);
+2  A: 

You can load the sheet in the Code Behind like @June suggests or you can load it in the aspx itself using inline code (frowned upon by some, but since a css file is layout logic I prefer to do it this way)

If you always load the second style sheet, just that you load a different one per user you could write something like this inside the head tag of your aspx page.

 <link href="<%= the name of your style sheet %>" rel="stylesheet" type="text/css" />

if you only put the style sheet in sometimes you can write this

<% if (whatever)
   { %>
    <link href="style.css" rel="stylesheet" type="text/css" />
<%} %>
Sruly
One small note - if you have `<head runat="server">` (which is the default) your example will often throw an exception. Use `<%#` instead and call the `.DataBind()` method to avoid it.
Keith
Why should this example throw an exception?
Sruly