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
Answers:
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
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
Method 2
You can do
<asp:Label runat="server" Text='<%# MyProperty %>' />
And then a Page.DataBind() in the codebehind.
Method 3
Leave the markup as is and make a call to Page.DataBind(); in your code behind.
Method 4
<asp:Label id="lID" runat="server"><%= MyProperty %></asp:Label>
since asp.net tags do not allow <% %> constructs, you cannot use Text=”<%= MyProperty %>”.
Method 5
<div> <%=MyProperty"%></div>
Method 6
Call lID.Databind() from code-behind
Method 7
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).
Method 8
You can do this:
<asp:Label ID="lblCurrentTime" runat="server">
Last update: <%=DateTime.Now.ToString()%>
</asp:Label>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0