开发者

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.


Default.aspx.cs

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

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.DataBind();
    }
}

Default.aspx

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


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.


Page.DataBind();

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜