IE8 beta 1 browser version targeting and ASP.NET Themes

I’ve moved this blog to I won’t be updating this space any longer.
Please update your RSS subscriptions to

You might find that when you add the meta tag required to force IE8 beta 1 into IE7 (or 6) rendering mode into your ASP.NET page (or Master Page) that it doesn’t work as expected…. IE8 continues to render the site in full standards mode. The reason might be that in beta 1 of IE8 the "X-UA-Compatible" meta tag must be the first element in the page head element and by default ASP.NET will insert the link tags to the CSS files in your theme at the beginning of the head element of your page (or Master Page), before any other elements that might actually be defined in the .aspx or .master file.

To fix this, add the following code to the page’s (or Master Page’s) PreRender event handler (Page_PreRender):

// Move browser tageting meta tag to top of header
HtmlMeta meta = null;
int metaXUACompatIndex = -1;
foreach (Control ctl in page.Header.Controls)
    meta = ctl as HtmlMeta;
    if (meta == null)

    if (meta.HttpEquiv == "X-UA-Compatible")
        // Grab index
        metaXUACompatIndex = page.Header.Controls.IndexOf(meta);
if (metaXUACompatIndex > 0)
    HtmlMeta metaXUACompat = page.Header.Controls[metaXUACompatIndex] as HtmlMeta;
    page.Header.Controls.AddAt(0, metaXUACompat);

This will move the browser targeting meta tag to the top of the list so that IE8 will honour it.


4 Comments on “IE8 beta 1 browser version targeting and ASP.NET Themes”

  1. Tatham says:

    What about sending it as an HTTP header instead of HTTP-Equiv? Does that work?

  2. Damian says:

    Yeah that should work too, you could just put it in the code behind then or configure IIS if you have access. I kinda like having it in the <head> as it’s in your face and forces other devs on the team to think about what it does. Plus when you have 10 different virtual environments your deploying the app to it’s easier than changing IIS on each server.