4

Overlooking something basic here but I am trying to set a variable and have it print in several places on the page. code behind:

public string myVariable { get {return "40"; } }

page:

<link rel="stylesheet" type="text/css" href="/css/main.css?v=<%=myVariable%>" />

output:

<link rel="stylesheet" type="text/css" href="/css/main.css?v=&lt;%=myVariable %>" />

It seems to have something to do with the quotes as this works when I take it outside of the href. I find that it works fine if I place a string in the code segement.

This works, but isn't what I want:

<link rel="stylesheet" type="text/css" href="/css/main.css?v=<%="40"%>" />

What is the logic behind this behavior and what do I need to do to make it work? I would also settle for a more elegant method of doing this.

LeRoy
  • 3,195
  • 4
  • 26
  • 23

5 Answers5

5

You need to single quote the html attribute like so:

<link rel="stylesheet" type="text/css" href='/css/main.css?v=<%=myVariable%>' />

I use this all the time especially within repeaters when I want to create anchor tags

<a href='PageToLinkTo.aspx?id=<%# DataBinder.Eval(Container.DataItem, "Id")%>'>Link Text</a>

This will only work in the body of your aspx page. If you have the link tag in the head section of your aspx page then check out this question for more info: Problem in Expression tag to bind string variable

Community
  • 1
  • 1
lomaxx
  • 113,627
  • 57
  • 144
  • 179
  • Single quotes don't fix it and a DataBinder is not involved. I definitely don't have a Container. – LeRoy May 19 '11 at 23:39
  • Use the fist code sample, it definitely does work.. the second code sample was just an example of how you can use it within a repeater. The key is that you have to setup your attribute like so href='link?paramval=<% anyServerVar %>' – lomaxx May 20 '11 at 00:20
  • 1
    Ok, so I'm gonna go out on a limb here and say that your link tag is inside the head tag on your aspx page... I had a similar problem a while back with another tag because asp.net treats links in the head tag as server controls. Check out this question for more info http://stackoverflow.com/questions/5603086/problem-in-expression-tag-to-bind-string-variable/5603410#5603410 – lomaxx May 20 '11 at 00:27
  • I pasted it directly into my page and the page renders - . I think the browser is swapping single for double quotes, but the server is not replacing the variable. – LeRoy May 20 '11 at 00:28
  • You are right. This problem is isolated to the link tags in the head. How handy. Your link was what I needed. Man, that is some weird behavior. – LeRoy May 20 '11 at 00:37
4

Why don't you just do like this:

<link rel="stylesheet" type="text/css" <%= ("href='/css/main.css?v=" + myVariable + "'") %> />
Safran Ali
  • 4,477
  • 9
  • 40
  • 57
  • This works but it doesn't make any sense why I should have to move the whole string into the code segment. This seems like a pretty basic need. – LeRoy May 19 '11 at 23:50
  • the reason i can give you for doing is that is because whatever is in this <% %> is written as an html or a text whenever the page is render back so the output is a string ... but when you put some code in href="<% %>", as browser tries to render them as link and when doing so if it encounters any special characters it converts them to thier respective codes ... and anyways why you worried about that whole href is generated like this, as seeing your code it is going to write myvariable ... so does it really matters that you write a single value or generate a whole href ???? – Safran Ali May 19 '11 at 23:56
  • The browser isn't involved in the rewriting that is taking place here. The reason it is important is why the rules change inside of quotes. ASP.NET is making a distinct about whether to interpret its own code block. A workaround like just throwing everything into the code block, while effective in this instance will lead to a great deal of my page to be a mash of double/single quotes in concatenated strings. – LeRoy May 20 '11 at 00:17
2

I actually had this same issue today and solved it by using a custom code expression builder.

Your code will look something like this:

<link rel="stylesheet" type="text/css" href="/css/main.css?v=<%$ Code:myVariable%>" />

A good tutorial that I used can be found here which I was able to modify to fit my application. This will also work if you need to add code inside of a server side control.

It was really easy to implement.

Here's what I added to my web.config:

   <compilation debug="true">
      <expressionBuilders>
        <add expressionPrefix="Code" type="CodeExpressionBuilder"/>
      </expressionBuilders>
   </compilation>

And in my App_Code folder I created ExpressionBuilder.vb:

Imports Microsoft.VisualBasic
Imports System.Web.Compilation
Imports System.CodeDom

<ExpressionPrefix("Code")> _
Public Class CodeExpressionBuilder
    Inherits ExpressionBuilder

    Public Overrides Function GetCodeExpression(ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, ByVal context As ExpressionBuilderContext) As CodeExpression
        Return New CodeSnippetExpression(entry.Expression)
    End Function

End Class

That was all I did to get it to work.

zeroef
  • 1,949
  • 23
  • 32
  • @LeRoy - Edited my answer to show everything I did. Sorry, it's in VB. – zeroef May 19 '11 at 23:48
  • 1
    I appreciate all the code. This just seems a little over kill. In reading the links you have it seems this is to handle inserting code blocks into server controls attributes/properties. I am really just trying to print to the page. Maybe my issue is related, but I shudder to think that this is what is necessary to do that. – LeRoy May 19 '11 at 23:54
  • 1
    This had more to do with my problem than I originally thought. Turns out asp.net was treating the link tag like a server control so this is a potential solution. – LeRoy May 20 '11 at 00:43
0

AFAIK, the whole property must be a code block, like:

href='<%= "css/main.css?v=" + myVariable %>'
LeRoy
  • 3,195
  • 4
  • 26
  • 23
Andomar
  • 232,371
  • 49
  • 380
  • 404
  • This does the same thing as my problematic case above. Worse in fact because it seems to grab a physical path and append it to the beginning. – LeRoy May 19 '11 at 23:18
0

Try this:

<link rel="stylesheet" type="text/css" href=<%="/css/main.css?v="+myVariable %> />
System Down
  • 6,192
  • 1
  • 30
  • 34
  • 1
    or possibly ` />` – ic3b3rg May 19 '11 at 22:55
  • Actually, in order to get all the quotes I need I can do this.. />. This seems pretty ridiculous though and I still don't understand why a property won't work in this instance but a string literal will. – LeRoy May 19 '11 at 23:11
  • Your original href is parsed by JS as a string (nothing hits the server). This version returns a string that's produced by the server. Make sense? – ic3b3rg May 19 '11 at 23:55
  • @ic3b3rg By JS do you mean Javascript? No javascript involved. Both my setup and this answer are totally performed on the server. – LeRoy May 20 '11 at 00:10
  • you're right... what I meant was it's interpreted as an HTML string – ic3b3rg May 20 '11 at 00:16