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.

3
  • 1
    Strictly speaking you have a public property with a getter, not a variable. Commented May 19, 2011 at 22:47
  • Out of curiosity, is the link tag going inside the head tag of your aspx page? Commented May 20, 2011 at 0:24
  • The link tag is in the head of my page. I see how that is important now. Commented May 20, 2011 at 0:41

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

5 Comments

Single quotes don't fix it and a DataBinder is not involved. I definitely don't have a Container.
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 %>'
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 stackoverflow.com/questions/5603086/…
I pasted it directly into my page and the page renders - <link rel="stylesheet" type="text/css" href="/css/main.css?v=&lt;%=myVariable%>" />. I think the browser is swapping single for double quotes, but the server is not replacing the variable.
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.
4

Why don't you just do like this:

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

3 Comments

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.
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 ????
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.
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.

3 Comments

@LeRoy - Edited my answer to show everything I did. Sorry, it's in VB.
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.
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.
0

Try this:

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

5 Comments

or possibly <link rel="stylesheet" type="text/css" href=<%="""/css/main.css?v="+myVariable+"""" %> />
Actually, in order to get all the quotes I need I can do this.. <link rel="stylesheet" type="text/css" href=<%="\"/css/main.css?v="+myVariable+"\"" %> />. 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.
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 By JS do you mean Javascript? No javascript involved. Both my setup and this answer are totally performed on the server.
you're right... what I meant was it's interpreted as an HTML string
0

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

href='<%= "css/main.css?v=" + myVariable %>'

1 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.