繁体   English   中英

asp.net数据绑定标签控件文本到页面基类的属性

[英]asp.net databind label control text to property of page baseclass

当我尝试将TestString输出到Label中时,为什么它总是空的?

所有asp.net页面的基类

public class PageBase : System.Web.UI.Page
{
    protected string TestString { get; set; }
}

protected override void OnPreInit(EventArgs e)
{
TestString = "test string";
}

从PageBase派生并使用母版页的asp.net页。

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="lblContent" runat="server" Text="<%# this.TestString %>" />
</asp:Content>

您需要调用DataBind()方法。

在page_load处理程序中,

TestString="Testing a property";
lblContent.DataBind();
//or
DataBind();

编辑

下面的代码实际上并不起作用-我对其进行了测试,只是效果不佳。 OP真正想要的是:

<div id="lblContent"><%= this.TestString %></div>

我认为你需要改变这个

<asp:Label ID="lblContent" runat="server" Text="<%# this.TestString %>" />

对此

<asp:Label ID="lblContent" runat="server" Text="<%= this.TestString %>" />

<%#仅运行代码,但不输出任何内容。 <%=将输出这些标记内的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM