【问题标题】:I want to get property value from code behind我想从后面的代码中获取属性值
【发布时间】:2011-07-29 18:15:49
【问题描述】:

我有一个案例,我需要在 aspx 页面中为 asp 标签设置 Text 属性,而不是来自后面的代码。更确切地说,我需要在aspx页面中为asp控件设置一个值,这个值是由后面同一页面代码中的一个属性设置的。

所以我需要使用一个表达式来做到这一点:

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

我用:

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

【问题讨论】:

  • 调试时,是否看到后面的代码中设置了“MyProperty”?

标签: asp.net


【解决方案1】:

默认.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();
    }
}

默认.aspx

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

【讨论】:

  • 它可以工作,但是它会将页面上的值写为 html span,但如果我在标签标签中使用它就不起作用!
【解决方案2】:

您必须区别对待常规 HTML 和 WebControl:


常规 HTML

使用&lt;%= ... %&gt; 就足够了:

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

WebControls(以 <...>

&lt;&lt;&gt;&gt;

&lt;&gt;

【讨论】:

【解决方案3】:
Page.DataBind();

你在你的代码中调用它吗?它将代码中设置的所有变量绑定到您的页面。

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2010-10-24
    相关资源
    最近更新 更多