Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development

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 on Wednesday, November 14, 2007 12:17 AM ASP.NET , CSS/DHTML/JavaScript | Back to top


Comments on this post: Code Render Blocks does not work inside HEAD server controls

# re: Code Render Blocks not always work inside server controls
Requesting 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.
Left by Bert on Nov 14, 2007 3:37 AM

# re: Code Render Blocks not always work inside server controls
Requesting 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.

Left by Michael Freidgeim on Nov 14, 2007 5:10 PM

# re: Code Render Blocks not always work inside server controls
Requesting 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... :-\
Left by Mufasa on Jan 24, 2008 10:29 AM

# re: Code Render Blocks not always work inside server controls
Requesting 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.
Left by Mufasa on Jan 24, 2008 10:35 AM

# re: Code Render Blocks not always work inside server controls
Requesting Gravatar...
Mufasa,
Thanks, create a Literal control is a good option.
I will try to fill it in server code.
Left by Michael Freidgeim on Jan 25, 2008 4:01 PM

# re: Code Render Blocks does not work inside HEAD server controls
Requesting 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...
Left by GlebVS on Jun 16, 2008 6:51 AM

# re: Code Render Blocks does not work inside HEAD server controls
Requesting 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.
Left by GlebVS on Jun 16, 2008 6:58 AM

# Insert CSS link with updatable version number in URL
Requesting 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....
Left by Michael Freidgeim on Feb 12, 2009 9:17 PM

# re: Code Render Blocks does not work inside HEAD server controls
Requesting 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);
Left by Ronen on Aug 11, 2013 6:09 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free