3

Basically I would like to find a way to ddo something like:

<asp:Label ID="lID" runat="server" AssociatedControlID="txtId" Text="<%# MyProperty %>"></asp:Label>

I know I could set it from code behind (writing lId.Text = MyProperty), but I'd prefer doing it in the markup and I just can't seem to find the solution. (MyProperty is a string property) cheers

8 Answers 8

11

You can do

<asp:Label runat="server" Text='<%# MyProperty %>' />

And then a Page.DataBind() in the codebehind.

1
  • You left out the % on the left... should be: Text='<%# MyProperty %>' Regardless, thanks. You have the only answer which is 99% correct. :) Also worth noting, you can do this: Text='<%# myClass.MyProperty %>'
    – maplemale
    Aug 25, 2014 at 0:44
10

Code expressions are an option as well. These can be used inside of quotes in ASP tags, unlike standard <%= %> tags.

The general syntax is:

<%$ resources: ResourceKey %>

There is a built-in expression for appSettings:

<%$ appSettings: AppSettingsKey %>

More info on this here: http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx

2
  • I do not think this is an answer for the question since the question specifically asks for the value of a property to be displayed in a label. I do not see how this method provides a way to achieve that. Sep 17, 2008 at 10:30
  • using the CodeExpressionBuilder, I could easily display the value of the property on the label. Just check out that blog post, it's quite useful.
    – Adam Vigh
    Sep 19, 2008 at 14:21
4

Leave the markup as is and make a call to Page.DataBind(); in your code behind.

2
<asp:Label id="lID" runat="server"><%= MyProperty %></asp:Label>

since asp.net tags do not allow <% %> constructs, you cannot use Text="<%= MyProperty %>".

1
  • Thanks, I've gone with this solution, but even calling the Page.DataBind() works just fine.
    – Adam Vigh
    Sep 16, 2008 at 15:34
0
<div> <%=MyProperty"%></div>
2
  • Label control even does not render div tag. May be it would make sense if you suggested <span> but even that would not have the same effect since you avoid all properties of a Label and disallowing the opportunity to use themes. Sep 16, 2008 at 15:33
  • yeah, there's no need for a div in my case as I'm using the AssociatedControlId property of the control, to create a label for a textbox.
    – Adam Vigh
    Sep 16, 2008 at 15:37
0

Call lID.Databind() from code-behind

0

When you use <%# MyProperty %> declaration you need to databind it, but when using <%= MyProperty %> you don't (which is similar to just writing Response.Write(MyProperty).

0

You can do this:

<asp:Label ID="lblCurrentTime" runat="server">
    Last update: <%=DateTime.Now.ToString()%>
</asp:Label>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.