6

I'm trying to use the Response.Write() method to dynamically insert content in the < head > section of an aspx page. I need to inject a string value from a property on a code-behind object which is a link to my CSS file. However, it is not being processed properly at run time. The object is public on the class and is hydrated in the Page_Load() event. Down in the body of the page I can successfully inject other properties from the Corpoartion object with no problem at all.

Why does this not work in the < head > section?

This is the part that does not expand correctly:

<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />

Here is the entire < head > section:

<head runat="server">
    <title></title>
    <link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
    <script language="JavaScript" type="text/JavaScript" src="cntv_menu.js"></script>
    <script language="JavaScript" type="text/JavaScript" src="cntv_category.js"></script>   
</head>

What is the reason that this will not expand properly?

MattSlay
  • 6,877
  • 4
  • 39
  • 52
  • In case others stumble here, the accepted answer has a slight mistake: CAN be used inside a element, I use it all the time for comment, and <script> variables (only <link> and <meta> elements are known to be a problem). I only needed to give the <link> element an ID= property then set the href property in code-behind.</script> – JayRO-GreyBeard Sep 28 '15 at 17:18

4 Answers4

6

You can't use <%= %> inside a runat="server" tag, which your <head> tag is.

You can either change it to <%# %> and DataBind to it in the code-behind, or you can make the link tag runat="server", give it an id and assign the attribute from the code behind.

See this answer, which goes into the details.

Community
  • 1
  • 1
Richard Szalay
  • 78,647
  • 19
  • 169
  • 223
  • Thanks. You got me going. < head > had runat="server" already, so it turns out I did not need to add that to the link, but I did give the link an id="csslink" and then in codebehind, I call csslink.DataBind(). Works like a charm! – MattSlay Feb 18 '10 at 16:51
  • I realize that this is nearly a decade old, but I stumbled across it and felt it was worth noting that `` can't be used inside of a quoted identifier within a server control (e.g., within `

    `). In those cases—as counter-intuitive as it seems if you're thinking of the declarative syntax as HTML elements—you must remove the attribute quotes. E.g., ` rel="stylesheet" type="text/css" />`. This will still render with quotes around the attribute value.

    – Jeremy Caney Sep 30 '19 at 19:56
0

Use this:

this.myButton.Attributes.Add(attribute, value);

It worked for me :)

Nissa
  • 4,589
  • 8
  • 27
  • 37
Cristian
  • 21
  • 3
0

the best way to resolve this problem is using OnPreRender

Example:

First, define your tag:

<link href="~/css/your_default.css" type="text/css" runat="server" id="myCSS" />

And on the OnPreRender:

protected override void OnPreRender(EventArgs e){
     base.OnPreRender(e);
     myCSS.Attributes["href"] = "~/css/your_new.css";
}
tyrodeveloper
  • 101
  • 1
  • 5
-1

If you write out the full line you should be ok:

<%
Response.write("<link href=\"" + Corporation.PageStyleSheet + "\" rel=\"stylesheet\" />");
%>

P.S. My syntax may not be completely right, sorry in advance.

Luke Duddridge
  • 4,123
  • 31
  • 50
  • That won't work for the same reason that `` doesn't work. The code within a runat=server control is parsed as a server-control tree only. – Richard Szalay Feb 18 '10 at 16:50
  • Good idea for thinking outside the box (kind of), but, as stated, it still doesn't work. Sorry, man. – MattSlay Feb 19 '10 at 00:23
  • That's odd, because I have it working in a head runat="server" Does ASP.NET runat Server work differently from MVC? or is it because my head section is inside a masterpage anyone know? – Luke Duddridge Feb 19 '10 at 08:24
  • FYI, you shouldn't be using head runat="server" with ASP.NET MVC as doing so mixes control hierarchies (webforms) with MVC – Richard Szalay Mar 01 '10 at 09:11