How to use ASP.NET <%= tags in server control attributes?

This works:

<span value="<%= this.Text %>" />

This doesn’t work:
<asp:Label Text="<%= this.Text %>" runat="server" />

Why is that?

How can I make the second case work properly, i.e., set the label’s text to the value of the “Text” variable?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use Data binding expressions

<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now %>" ></asp:Label>

Code behind,
protected void Page_Load(object sender, EventArgs e){

Method 2

you can do this

 <asp:Label ID="Label1" runat="server" ><%= variable%></asp:Label>

Method 3

You will need to set the value of the server control in code

First of all, assign an ID to the label control so you can access the control

<asp:Label ID="myLabel" runat="server" />

Then, in your Page_Load function, set the value of your labels ‘Text’ field
protected void Page_Load(object sender, EventArgs e)
    myLabel.Text = 'Whatever you want the label to display';

This function will be in your code behind file, or, if you are not using the code behind model, inside your aspx page you will need
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
        myLabel.Text = 'Whatever you want the label to display';

Good luck.

Method 4

In my code i am using something like this easily but in the databound control like ListView Item template

 <asp:HyperLink ID="EditAction" class="actionLinks" Visible='<%#Eval("IsTrue").ToString() != "True"%>' runat="server" NavigateUrl='<%# Eval("ContentId","/articles/edit.aspx?articleid={0}")%>' />

But when i tried to use outside the databound control using <%# .. %>, it simply doesn’t work.

You can easily do with

<a href="<%=myHref%>" rel="nofollow noreferrer noopener">My href</a>

But for server controls, and outside of databound control. We need to call DataBind() in pageload event explicitly
<asp:Hyperlink ID="aa" NavigateUrl='<%#myHref%>' >

Method 5

Not sure how to mark this as such, but this is a bit of a duplicate. See this thread.

I don’t think embedding code in to your markup will really make your markup any clearer or more elegant.

Method 6

<asp:Label> is compiling at runtime and converting to html tags. You can set text with codebehind or like this:

<asp:Label id="Text1" runat="server" />
<% Text1.Text = this.Text;%>

UPD: Seems like my variant doesnt work, this is better:
protected void Page_Load(object sender,EventArgs e) 
    Text1.Text = this.Text;

Method 7

Just pitching this little nugget in for those who want a good technical breakdown of the issue —

I think the crux is in pretty decent agreement with the other answers:

  • The <%= expressions are evaluated at render time
  • The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
  • <%# expressions can be used as properties in server-side controls. <%= expressions cannot.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x