views:

22

answers:

1

Suppose I have the following markup in a standard ASP.NET 2.0 web form:

<head runat="server">
    <title>My Snazzy Page</title>
    <link type="text/css" href="<%= PathUtilities.AssetPath %>/css/page.css" rel="stylesheet" />
    <script type="text/javascript" src="<%=PathUtilities.AssetPath %>/lib/jquery/1.4.2/jquery.min.js"></script>
</head>

What's odd is that this renders the <link> element literally, with the embedded code brackets, while it interpolates the output of the same code into the script tag. In other words, the browser sees this:

<head><title>My Snazzy Page

</title><link type="text/css" href="&lt;%= PathUtilities.AssetPath %>/css/page.css" rel="stylesheet" />
<script type="text/javascript" src="/rmt/lib/jquery/1.4.2/jquery.min.js"></script>
</head>

Obviously the problem disappears if I remove the runat="server" from the head element.

A: 

Well what you're doing is (no offence) a bit silly, that is - having a <head> server-side element, with a nested <link> client-side element with server-side href attribute

You are dynamically rendering the href value from server code anyway, so a better solution would be to dynamically render the link tag from the server altogether.

Example (code-behind of page)

// Define an HtmlLink control.
HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = "/css/page.css";
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");

// Add the HtmlLink to the Head section of the page.
Page.Header.Controls.Add(myHtmlLink);

Your ASPX then becomes much neater:

<head runat="server">
    <title>My Snazzy Page</title>
    <!-- CSS/JS included dynamically -->
</head>
RPM1984
I agree it's odd and there is more than one reasonable alternative. I'm wondering if there's some underlying principle at play with interpolation of markup in built-in controls like head.
Thomas H
So im getting the impression you're not so much looking for a solution, but an explanation.
RPM1984