1

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.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
theraneman
  • 1,620
  • 4
  • 18
  • 32
  • Are you asking if the Request.Browser feature always figures out the browser properties and versions correctly? Or if that adding a `` via `Head.Controls.Add` is consistent? If the first, then no, the browser-detection feature is not infallible, especially as new browsers are released; though it should be fairly stable for detecting IE6 specifically if that's all you need to check. But to the second, yes, `Head.Controls.Add` is as reliable as the code you write for it is. – Jon Adams Sep 10 '11 at 01:52
  • Have you checked that you aren't caching the page output? You would see this behavior if you generate the page in one browser but it loads the page from the server cache without rechecking the browser details when loaded in another browser. – Jon Adams Sep 10 '11 at 01:54

1 Answers1

0

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
  • 73,608
  • 45
  • 233
  • 342
  • 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 Jul 27 '09 at 06:45
  • 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 Jul 27 '09 at 06:47
  • 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 Jul 27 '09 at 07:06