tags:

views:

2607

answers:

8

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

A: 

Call lID.Databind() from code-behind

Akselsson
A: 

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

John Sheehan
+1  A: 
<asp:Label id="lID" runat="server"><%= MyProperty %></asp:Label>

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

Serhat Özgel
Thanks, I've gone with this solution, but even calling the Page.DataBind() works just fine.
snomag
A: 
<div> <%=MyProperty"%></div>
DevelopingChris
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.
Serhat Özgel
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.
snomag
+2  A: 

You can do

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

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

Mark S. Rasmussen
A: 

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).

Frederik Vig
+2  A: 

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

mrflippy
This's more elegant, thanks for the info
snomag
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.
Serhat Özgel
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.
snomag
A: 

You can do this: Last update: <%=DateTime.Now.ToString()%> no problem...

Brian