Code Render Blocks does not work inside HEAD server controls

I had a ASP.NET page with HEAD as server control similar to the following:

      <HEAD runat = "server">
            <%this.RenderHeadItems();%>
            <link rel="stylesheet" type="text/css" href="WebjetStyles.css?v=<%=AssemblyVersionNumber()%>">
      </HEAD>
The first server function was called and executed successfully, but the second Code Render Blocks located inside a parameter of link element, wasn't considered as server code and literally was copied to client HTML as &lt;%=AssemblyVersionNumber()%&gt;
Only when I removed runat = "server" attribute in HEAD element, the second Code Render Block was executed and rendered to HTML correctly.
I haven't find any documented limitations regarding Embedded Code Render Blocks in MSDN
However other people noticed similar problems with HEAD runat = "server".
And in some scenarios you have to use HEAD runat = "server".

I am calling AssemblyVersionNumber() to ensure that browser will load the latest version of CSS file.

Previously I posted about other  unexpected behaviour of controls with runat = "server":
asp:PlaceHolder can be put within html table, if it is not runat="server"
See my post Insert CSS link with updatable version number in URL how I resolved the issue.
posted @ Wednesday, November 14, 2007 12:17 AM
Print

Comments on this entry:

# re: Code Render Blocks not always work inside server controls

Left by Bert at 11/14/2007 3:37 AM
Gravatar
I've usually found that I need to put href='' instead of double quotes href="blah" around the <%= %> block to get it to work right. we've got runat=server on our head tag, but in our Master page.

# re: Code Render Blocks not always work inside server controls

Left by Michael Freidgeim at 11/14/2007 5:10 PM
Gravatar
Bert,
Thank you for suggestion.I've tried to use single quotes instead of double quotes, but it has no differences. Actually if I have HEAD runat="server", the single quotes are converted to double quotes in generated HTML.

# re: Code Render Blocks not always work inside server controls

Left by Mufasa at 1/24/2008 10:29 AM
Gravatar
Actually, he meant use the single quotes around the ASP script block within the HEAD tag. So:

<head runat="server">
<link href='file.css?v=<%= code %>' ...>
...

Although, I think that fails too. Top of my head I think you need:

<head runat="server">
<link href='<%= "file.css?v=" + code.ToString() %>' ...>
...

I could be wrong. Too lazily to run the code right now... :-\

# re: Code Render Blocks not always work inside server controls

Left by Mufasa at 1/24/2008 10:35 AM
Gravatar
I ran the code and it didn't work either. Even with .Net 2.0 data binding syntax (<%# ... %>). I know it does work in other places, just not the <head> server tag.

Anyway, you can still just create a Literal control in the code-behind, fill it with the raw HTML, and add it to Page.Header.Controls collection. I know that works too. Not as convenient, true... But it works.

# re: Code Render Blocks not always work inside server controls

Left by Michael Freidgeim at 1/25/2008 4:01 PM
Gravatar
Mufasa,
Thanks, create a Literal control is a good option.
I will try to fill it in server code.

# re: Code Render Blocks does not work inside HEAD server controls

Left by GlebVS at 6/16/2008 6:51 AM
Gravatar
Encountered the same problem as described here.
Indeed, none of the solutions cited above seem to work.
I found a way that does render the desired HTML:

<head runat="server">
...
<link href=<%= "\"" + serverName %>/css/doc.css" ...>
...
</head>

As fas as I understand the problem was this:
when encountering < inside "" (quotation marks), ASP.NET converted < (the symbol) into the &lg; (HTML entity). So, the solution above avoids this by escaping the leading quotation mark inside of the render block.
While the above logic might seem to make sense, it is not clear why this is different in the <head> vs <body> sections.
Note: the <head... in my example does use runat="server" but my page does not use any masterpages. But it should still work...

# re: Code Render Blocks does not work inside HEAD server controls

Left by GlebVS at 6/16/2008 6:58 AM
Gravatar
Another note... for completeness' sake:

in the example above, if the head server control contains a script, such as
<script src="<%= serverName %>/js/doc.js" ...>,
there is no problem with having a literal quotation mark preceding the (inline) render block.

# Insert CSS link with updatable version number in URL

Left by Michael Freidgeim at 2/12/2009 9:17 PM
Gravatar
More than a year ago I wrote a post Code Render Blocks does not work inside HEAD server controls about the problem, that prevented me to update URL of CSS file, when version of assembly incremented....

# re: Code Render Blocks does not work inside HEAD server controls

Left by Ronen at 8/11/2013 6:09 PM
Gravatar
I had the same behavior, it messed my whole html. It should be reported as a bug, because no compilation error appears. A solution to the problem is to add it in Page_Init in the code behind. like this to add a link element:

HtmlLink htmlLink = new HtmlLink();
htmlLink.Href = "...";
htmlLink.Attributes.Add("rel", "stylesheet");
htmlLink.Attributes.Add("type", "text/css");
head1.Controls.Add(htmlLink);

Your comment:



(not displayed)

 
 
 
 
 

Live Comment Preview:

 
«August»
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456