Joao Silva
Joao Silva

Reputation: 876

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?

Upvotes: 44

Views: 85249

Answers (7)

Bondolin
Bondolin

Reputation: 3121

Just pitching this little nugget in for those who want a good technical breakdown of the issue -- https://blogs.msdn.microsoft.com/dancre/2007/02/13/the-difference-between-and-in-asp-net/

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.

Upvotes: 1

chevett
chevett

Reputation: 728

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.

Upvotes: 6

user1855575
user1855575

Reputation: 167

you can do this

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

Upvotes: 10

Sachin Kumar
Sachin Kumar

Reputation: 996

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%>">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%>' >

Upvotes: 9

KV Prajapati
KV Prajapati

Reputation: 94635

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){
  DataBind();
}

Upvotes: 57

x2.
x2.

Reputation: 9668

<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;
}

Upvotes: 6

RR.
RR.

Reputation: 679

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';
    }
</script>

Good luck.

Upvotes: 8

Related Questions