I'm trying" /> My Page I'm trying" /> My Page I'm trying"/>

How to dynamically add quirks mode meta-tags in ASP.NET Web Forms from code behind?

368 Views Asked by At

I've got an.aspx page like the following:

<!DOCTYPE html>
<html>
<head runat="server">
    <title>My Page</title>
</head>
<body></body>
</html>

I'm trying to set quirks mode meta tags in code behind of the page i.e. in Page_load using:

HtmlMeta _ContentTypeTag = new HtmlMeta()
{
    HttpEquiv = "Content-Type",
    Content = "text/html; charset=UTF-8"
};

Page.Header.Controls.Add(_ContentTypeTag);

HtmlMeta _QuirksModeTag = new HtmlMeta()
{
    HttpEquiv = "X-UA-Compatible",
    Content = "IE=EmulateIE8"
};

Page.Header.Controls.Add(_QuirksModeTag);

But when I see the page source, it currently outputs this:

<!DOCTYPE html>
<html>
<head runat="server">
    <title>My Page</title>
    <meta http-equiv="Page-Enter" content="blendTrans(Duration=0.0)">
    <meta name="MSThemeCompatible" content="no">
</head>
<body></body>
</html>

Whereas, I'm expecting the output to be something like this:

<!DOCTYPE html>
<html>
<head runat="server">
    <title>My Page</title>
    <meta http-equiv=Content-Type content="text/html; charset=UTF-8">
    <meta http-equiv=X-UA-Compatible content=IE=EmulateIE8>
</head>
<body></body>
</html>

What's the cause of this behavior and how I can fix it?

UPDATE

I've fixed this using Response.AddHeader("X-UA-Compatible", "IE=EmulateIE8"); instead. (Taken from here)

But, it'll still be good to know why the initial approach wasn't working.

1

There are 1 best solutions below

0
Snack'Eyes On

It's Too Simple, USe below code at code behind.

    Page.Title = "Meta title string here.."; 
    Page.MetaKeywords = "Meta keyword string here.."; 
    Page.MetaDescription = "Meta description string here..";