views:

378

answers:

2

Hi guys, I have the following piece of code to add the right CSS file in the "head" depending on browser,

    string browserName = Request.Browser.Browser;
    string browserVersion = Request.Browser.Version;
    Control Head = Page.Master.FindControl("stuHead");

    if (Head != null)
    {
        if (browserName == "IE")
        {
            if (browserVersion == "6.0")
            {
                Head.Controls.Add(new LiteralControl("<link rel='stylesheet' rev='stylesheet' href='Home-IE6.css' type='text/css' media='all' />"));
            }
            else
            {

                Head.Controls.Add(new LiteralControl("<link rel='stylesheet' rev='stylesheet' href='Home-IE.css' type='text/css' media='all' />"));
            }
        }
        else
        {
            Head.Controls.Add(new LiteralControl("<link rel='stylesheet' rev='stylesheet' href='Home.css' type='text/css' media='all' />"));
        }
    }
    else
    {
        Response.Write("<link rel='stylesheet' rev='stylesheet' href='Home.css' type='text/css' media='all' />");
    }

When I open my page in IE8, sometimes I see the Home.css, actually I should be seeing the Home-IE.css. I have ensured that the Head is not null. Not sure if anyone has experienced such a thing. Any comments appreciated.

A: 

I wonder why don't you do it in HTML part... there are plenty of websites explain this css trick.

<!--[if IE]>
  <link rel="stylesheet" type="text/css" href="all-ie.css" />
<![endif]-->


<link rel="stylesheet" type="text/css" href="common.css" />

<!--[if IE]>
  <link rel="stylesheet" type="text/css" href="all-ie.css" />
<![endif]-->

<!--[if IE 6]>
  <link rel="stylesheet" type="text/css" href="ie-6.0.css" />
<![endif]-->

<!--[if lt IE 6]>
  <link rel="stylesheet" type="text/css" href="ie-5.0+5.5.css" />
<![endif]-->


just to point out something on your code, try to learn how to Refactor and how to Reuse code.

string browserName = Request.Browser.Browser;
string browserVersion = Request.Browser.Version;

if (Head != null)
{
    if (browserName == "IE")
    {
        if (browserVersion == "6.0")
            AddCSS("Home-IE6.css", false);
        else
            AddCSS("Home-IE.css", false);
    }
    else
        AddCSS("Home.css", false);
}
else
    AddCSS("Home.css", true);

...

void AddCss(string cssFile, bool write) {
    Control Head = Page.Master.FindControl("stuHead");
    String css = String.Format("<link rel='stylesheet' rev='stylesheet' href='{0}' type='text/css' media='all' />", cssFile);

    if(write)
        Response.Write(css);
    else
        Head.Controls.Add(new LiteralControl(css));
}
balexandre
The issue is I need to add different CSS files on different pages. So I have added this code inside a usercontrol load event for the Home page.
theraneman
If you find yourself in that position, it tells me that your doing something wrong. You should never do that, remember that you can overwrite a css using the !important keyword.
balexandre
Sure, I do realize that my code is not final yet:), my question was does adding such a CSS in head of a page work all the time? IE does show different behaviour sometimes.
theraneman
A: 

Check browserName value when you get Home.css. It must be something else than "IE".

Vili
I did check the value, it is IE.
theraneman