Tutorial :Why does the runat-attribute in the head-tag change the way meta tags are handled in ASP.NET?



Question:

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.


Solution:1

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);  


Solution:2

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »