views:

208

answers:

2

Example 1:

<head>
  <meta http-equiv="description" content="<%= Foo %>"/>
</head>

Renders

<meta http-equiv="description" content="Bar"/>

Example 2:

<head runat="server">
  <meta http-equiv="description" content="<%= Foo %>"/>
</head>

Renders:

<meta http-equiv="description" content="&lt;%= Foo %>"/>

Note the discrepancy, the < has become &lt; but the > remains the same.

There are some questions on this topic, and the answers are the workarounds, but nobody seems to know why this happens.

+3  A: 

You can't have a script tag (<% = %>) inside a server control, that's why it's turned into plain text instead of being executed.

You can add the meta tag from the code behind like this:

HtmlMeta meta = new HtmlMeta();
meta.HttpEquiv = "description";
meta.Content = Foo;
Page.Header.Controls.Add(meta);
Guffa
But <title><%= "Hello, world!" %></title> does work in a head tag with the runat attribute set to "server"
michielvoo
Title can be controlled through Page.Title as it is.
Broam
+1  A: 

When you add runat=server the Head tag becomes a server control. I guess based on your results that the contents of server controls are not parsed for inline substitutions using the <% ... %> syntax.

You could make the Meta tag a server control as well by adding the runat=server to it and access its attributes programmatically.

Josh Einstein