41

Is it possible to do something like this in a head tag, of master page, which has runatserver:

 <link rel="Stylesheet" type="text/css" href='<%=Config.ResourcesDomain %>/images/style.css' />

This is not working, as it produces this kind of html:

<link rel="Stylesheet" type="text/css" href="&lt;%=Config.ResourcesDomain %>/images/style.css" />

7 Answers 7

76

The reason the output is being rendered like so:

href="&lt;%=Config.ResourcesDomain %>/images/style.css"

Is because ASP.NET is treating the link as an HtmlLink control, and rendering the contents of the href attribute as a literal.

This is a strange quirk of marking the head section as a server control, where certain elements are treated as server controls (even without being marked explicitly with the runat="server" attribute).

Removing the quotations around the href attribute resolves the issue:

href=<%= Config.ResourcesDomain %>/images/style.css

Doing so stops the link element being treated as a server control, thus executing the code block and rendering the correct URL.

However, the above writes the href value out without quotes. Using the following, will add the quotes to the link tag:

href=<%= String.Format("'{0}'", Config.ResourcesDomain) %>/images/style.css

Hope this helps.

Edit

Strangely, if you use double quotes for the href attribute, and include double quotes within the code block this also resolves the issue:

href="<%= "" + Config.ResourcesDomain %>/images/style.css"

However, none of the above are particularly elegant solutions, and setting the URL from the code behind is probably the way to go.

3
  • 2
    Doing href="<%# "" + MyString %>" worked for me. Thank you so much.
    – Hanna
    Nov 13, 2013 at 18:16
  • I was going out of my mind! Thanks!!! I needed it to write the correct CSS file based on the user...
    – AndreaCi
    Feb 8, 2016 at 8:56
  • href="css/Login2.min.css?v<%= "" + AppSettingHelper.Version %>" is working for me Aug 15, 2018 at 16:05
14

Another solution I've found here: https://stackoverflow.com/a/5727996/368613 -- just place code inside PlaceHolder:

<asp:PlaceHolder runat="server">
  ... your code with <%= %> tags ...
</asp:PlaceHolder>
2
  • 1
    This is the only working solution I found. Nothing else worked Dec 18, 2020 at 6:30
  • Echoing this is the only thing that worked for me
    – Corey
    Feb 4, 2022 at 17:33
4

mark-up

<head>
   <asp:Literal ID="litHead" runat="server" />
</head>

code-behind:

on page_load

litHead.Text = "<link rel='Stylesheet'  type='text/css' href='" + Config.ResourcesDomain + "/images/style.css' />";

Update: do this then

<head runat="server">
    <%
        Response.Write("<link rel='Stylesheet'  type='text/css' href='" + Config.ResourcesDomain + "/images/style.css' />");
    %>
    <title></title>
</head>
2
  • 1
    The goal is to set it inline in the head tag, without code behind
    – jekcom
    Nov 12, 2011 at 12:48
  • see updated above. is this some kind of a competition that you need to do it without code behind? As much as possible, avoid using the what we called "Spaghetti Coding" or the inline coding. Those style of coding should be forgotten as those are frequently used back in asp days. ASP.NET beautifully separated the markup and the codes to better practice coding standards.
    – Alvin
    Nov 12, 2011 at 13:08
1

change it to

 <link rel="Stylesheet"  type="text/css" href='<%Response.Write(Config.ResourcesDomain); %>/images/style.css' />

It should work

3
  • it still escapes the "<" sign and makes it &lt;%Response.Write(Config.ResourcesDomain); %>/images/style.css
    – jekcom
    Nov 12, 2011 at 12:49
  • do you write it in an aspx page ?
    – DeveloperX
    Nov 12, 2011 at 12:51
  • Can you put more all code of master page ,but not the body just the upper part of body
    – DeveloperX
    Nov 12, 2011 at 12:53
1

Remove the runat="server" attribute on the opening head tag. This way the asp.net inline code is correctly rendered.

0

Or do something like this:

<head>
    <style type="text/css">
        @import "<%= ResolveUrl("~/content/styles.css") %>";
        @import "<%= ResolveUrl("~/content/print.css") %>" print;
    </style>
</head>
0

Apparently data binding is required when using the inline tag "<%# %>".

<head id="Head">
    //Stuff with inline code
</head>

Code Behind:

protected void Page_Load {
    Head.DataBind();
}

Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.