2

I am trying to conditionally include a css file depending on what browser the user is using. And I've been able to get it to work one way but not the way I want because it will require an extra style sheet. The way that works, in my Site.Master file I include a style sheet for ie8.

<link href="~/Styles/ie8.css" rel="Stylesheet" type="text/css" />

Then in Site.Master.cs in the Page_Load function.

    if (!(browser.Type == "IE8" ||
        browser.Type == "IE7" ||
        browser.Type == "IE6" ||
        browser.Type == "IE5"))
    {
        Page.Header.Controls.Add(new LiteralControl(
            "<link rel=\" stylesheet\" type=\"text/css\" href=\"" + ResolveUrl("~/Styles/ie.css") + "\" />"));
    }

This works and the css is correct and using developer tools and compatibility in ie10 I can see it includes the correct css. But If I change my code to what should be equivalent. It doesn't work. Site.Master:

<link href="~/Styles/ie.css" rel="Stylesheet" type="text/css" />

Site.Master.cs

    if ((browser.Type == "IE8" ||
        browser.Type == "IE7" ||
        browser.Type == "IE6" ||
        browser.Type == "IE5"))
    {
        Page.Header.Controls.Add(new LiteralControl(
            "<link rel=\" stylesheet\" type=\"text/css\" href=\"" + ResolveUrl("~/Styles/ie8.css") + "\" />"));
    }

If I put a breakpoint at Page.Header.Controls.Add I can see the function is getting called when in compatibility mode in ie10. But then when I go to dev tools and look for elements that use classes defined in ie8.css they aren't there like it was never included. What is going on here?

1
  • Why do you need to use dev tools and breakpoints to see if the stylesheet was included? Just view source. Commented Apr 17, 2013 at 14:25

1 Answer 1

1

Instead of checking for IE serverside, you should use conditional comments:

<!--[if lte IE 8]><link href="ie8.css" rel="Stylesheet" type="text/css" /><![endif]-->
<!--[if lte IE 7]><link href="ie.css" rel="Stylesheet" type="text/css" /><![endif]-->
Sign up to request clarification or add additional context in comments.

5 Comments

I've seen suggestions for that but I never could get them working. What I saw what this css-tricks.com/how-to-create-an-ie-only-stylesheet . When I entered it into <!--[if lte IE 8]> <![endif]--> Visual studio seems to think it is a comment. Unless I am entering it into the incorrect place. I rather do it client side if I could get that working.
If you aren't between the head tags (or a ContentPlaceHolder that is in the head) then you are in the wrong place. It should work fine in VS.
It works. I like this solution better, despite it not answering the question. What is completely baffling is why would the label it as a comment when it actually does something. That is not a comment if it has an effect on the program. And why the server side doesn't work makes no sense either.
It is a comment. Only the dirty ones from Redmond recognize these statements. That's why they're called "conditional comments".
hi @DiegoNolan, I cant still make it work. I have added it like this on my master <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <%@ Register Src="~/Shared/Header.ascx" TagName="Header" TagPrefix="Admin" %> <%@ Register Src="~/Shared/Footer.ascx" TagName="Footer" TagPrefix="Admin" %> <!DOCTYPE html> <html xmlns="w3.org/1999/xhtml"> <head runat="server"> <!--[if lte IE 8]><link href="Shared/Styles/ie.css" rel="Stylesheet" type="text/css" /><![endif]--> </head> <body> Can you let me know how you fixed it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.