Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

may1hem

Programmer
Jul 28, 2002
262
GB
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?

Thanks,
May
 
May: Try:

<asp:Label id="myLable" runat="server" Text=<%=myVar%>/>
 
May: one more note. You can use it directly in the html as follows:

<td align="center">Bring <%=myVar%> on the 15th.</td>

etc.
 
May sorry for the excessive posting - was in a bit of a hurry. I thought I had read that you were having success in other areas of the web page but not the asp:Labels.

You might try declaring the variable as Public if you are losing it on a Postback, etc. So long as the variable is declared and available it should populate OK. Try declaring the variable Public if you are loosing it on PostBack.

I would elminate for starters the quote tags.

 
That is classic ASP code. Use .NET syntax:

Label.Text = MyVar


Jim
 
Jim:

Good point. I have the <%=myVar%> in use in several places as it picks up new strings but accomplishing this in code behind in all cases makes a lot of sense. You can use javascript to populate a string on the client side.

After thinking about this I have to agree; there is really no reason why you could not make all assignments in code behind and elminiate completely the capture of a variable in this method.
 
Jim:

I did think of one example where it might prove useful beyond simple binding (such as in a DataList, etc), for example:

<Font size="12px" color=<%=strColor%>>My Text.</Font>

Perhaps in cases where you are controlling the properties of html elements you may want to use it. But I agree with your assesment; that when you can easily assign a string in code behind it is preferrable.
 
Somtimes it is just hard to break away from what you know. The .NET architecture is quite a differnt style of programming and takes time to get used to if you have been using classic asp for a long time. I think this could be the siutation here.
 
I don't think you can use the variables within .NET controls, you have to create a function in the script and then call the function within the control and make sure you bind the control with the function in the page load routine
 
Code:
EditButton.DataBind()

Function GetColor() As String
  ButtonColor="RoyalBlue"
  Return ButtonColor
End Function

<asp:button
  id="EditButton"
  Text=<%#GetColor()%>
  Runat="Server" />
 
You shouldn't really use <% %> tags at all if you can help it. They are needed sometimes (such as in DataGrid controls to bind a column) but in all of the examples posted so far on this thread (e.g. a label, a button and a font size(which by the way should be avoided anyway!)) they are not needed and should always be done in the code-behind file.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Works better with single quotes.
<asp:label text='<%=myVar %>' />
You also need the runat=server or the control will not render if this is inteded to be a asp:label.

Make sure myVar is Public and not Private and that it is assigned it's value on Page_Load.

 
Although, as Jim has pointed out, that is more of the classic ASP way of assigning variables and it should be done in a seperate code file.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm: I agree that assignment of strings should be carried out in code behind as pointed out (indeed I spent this morning stripping the handful of '<%=xx%>' occurrences and replacing them with simple code assignments - as Jim pointed out; old habits are sometimes elusive as well as hard to break.
 
Yes, I saw that you agreed to that after Jim posted it; my last comment was directed at the post made by RTomes.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
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,
May
 
Mary --

It's probably your double quotes. However, I would heed the advice given in this thread and in your code behind you should have:

Code:
Sub Page_Load....
 If Not IsPostBack Then...
  Label1.Text = System.DateTime.Today.Day.ToString 
 ...
 ...
 End If
End Sub
As suggested, you really should try and avoid this approach -- indeed, the few places I had it in my own code (older code) I replaced this weekend with code statements.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top