title: Conditionally Including css in C# ASP.net Webforms
body: <p>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.</p>
  
  <pre><code>&lt;link href="~/Styles/ie8.css" rel="Stylesheet" type="text/css" /&gt;
  </code></pre>
  
  <p>Then in Site.Master.cs in the <code>Page_Load</code> function.</p>
  
  <pre><code>    if (!(browser.Type == "IE8" ||
          browser.Type == "IE7" ||
          browser.Type == "IE6" ||
          browser.Type == "IE5"))
      {
          Page.Header.Controls.Add(new LiteralControl(
              "&lt;link rel=\" stylesheet\" type=\"text/css\" href=\"" + ResolveUrl("~/Styles/ie.css") + "\" /&gt;"));
      }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>&lt;link href="~/Styles/ie.css" rel="Stylesheet" type="text/css" /&gt;
  </code></pre>
  
  <p>Site.Master.cs</p>
  
  <pre><code>    if ((browser.Type == "IE8" ||
          browser.Type == "IE7" ||
          browser.Type == "IE6" ||
          browser.Type == "IE5"))
      {
          Page.Header.Controls.Add(new LiteralControl(
              "&lt;link rel=\" stylesheet\" type=\"text/css\" href=\"" + ResolveUrl("~/Styles/ie8.css") + "\" /&gt;"));
      }
  </code></pre>
  
  <p>If I put a breakpoint at <code>Page.Header.Controls.Add</code> 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?</p>