I want to get property value from code behind

I have a case that I need to set the Text property for an asp label in the aspx page not from code behind. More exactly, I need to set a value to asp control in aspx page and this value is set by a property in the same page code behind.

so I need to use an expression to do that like:

<asp:Label Text="<%= MyProperty %>" ..../>

I use:
<%= MyProperty %> doesn't work.
<%# MyProperty %> doesn't also.


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


public partial class _Default : System.Web.UI.Page
    public string CustomTitle = "This Is Title";

    protected void Page_Load(object sender, EventArgs e)

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

Method 2

You have to treat regular HTML and WebControls differently:

regular HTML:

Using <%= ... %> is sufficient:

<span><%= MyProperty %></span>

WebControls (stuff starting with <asp:…>):

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

In this case, you also have to call Me.DataBind() (VB) or this.DataBind(); (C#) in your codebehind, since <%# ... %> are data binding expressions.

Method 3


Do you call this in your code? It binds all variables set in code to your page.

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

0 0 votes
Article Rating
Notify of

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