How to insert a variable into HTML code? <%=myVar %> doesn't evaluate?

I need to display a variable’s value on a page.

I’m trying to use <label text=“<%=myVar %>”> but this doesn’t seem to evaluate. The label on the web page shows the code itself: “<%=myVar %>”.

This is not specific to the label tag. I use <%=myVar %> in other parts of the page but these don’t evaluate either.

Any ideas?


Where do you set the value of the variable?
PS What language are you using?

The variable needs to be declared as protected or public; private variables are not avaliable for that operation.

Also, use databinding (<%# MyVar %>) rather than Response.Write (<%= MyVar %>) wherever possible.

Why not just set the label text programatically. i.e.

In your markup:

<asp:Label id=“test” />

In your code in the page load event, or which ever event you want to display the variable:

test.Text = variable.ToString();

This is C# code, but you should be able to figure out vb.

There are a few good reasons not to use a Label when you don’t need programmatic control:

  1. Resource usage–rendering the label costs far more server resources than simple databinding. The object needs to live somewhere.
  2. Resource usage–labels do put themselves in viewstate unless you turn it off.
  3. Labels generate span tags around them, which are often unnecessary.

May be a dumb question but it is an page and not just a html page? lol.

So, if i understand you correct, the test of the label renders as <%#=myvar%>? Interesting.

This is what i just tried in an aspx page…

dim myVar

myVar = “Some Text”


I tried this…<label text=“<%=myVar%>”></label> but it didnt work. maybe cause intellasense didnt recogonize ‘text’ as an actual property of the label.

No, an ASP.NET page is not a simple html page, but rather a very beefy and complex object. And there is a big difference between a <label> and an <asp:Label>.

c# <% Response.Write(myVar); %>
vb <% Response.Write(myVar) %>

Keeping in mind that asp:labels are infact html span’s. If you want to get more complex you can add runat=“server” to any control and pretty much control it from the codebehind.

So if I want to use a div to display something, i would use

<div runat=“server” id=“div1”>test</div>

Then switch to the designer and back to code, and you will get a new object in your declarations, now you can call code within visual studio using div1.InnerHtml = “Test 2” and on postback it would make the requested changes.

Using <ASP:Label> is just a fancy way of putting <span runat=“server” id=“cntrlName”></span>


Do not use Response.Write() unless you absolutely have to. Its an archaic vestigal method from a bygone era.

Thanks for your help. I tried out these various methods this weekend. It works fine now.

The only thing that causes a problem is when I try to insert a variable inside a text string.

E.g. this doesn’t work:
<asp:Label ID=“Label1” runat=“server” Text=“<%=System.DateTime.Today.Day.ToString %>”></asp:Label>

I know now that I should use code behind but it surprises me that this won’t work.

Thanks again,

<asp:Label ID=“Label1” runat=“server” Text=“<%=System.DateTime.Today.Day.ToStringB[/B] %>”></asp:Label>