[英]asp.net databind label control text to property of page baseclass
Why is TestString is always empty when I try to output it into my Label? 当我尝试将TestString输出到Label中时,为什么它总是空的?
Base class for all asp.net pages 所有asp.net页面的基类
public class PageBase : System.Web.UI.Page
{
protected string TestString { get; set; }
}
protected override void OnPreInit(EventArgs e)
{
TestString = "test string";
}
asp.net page that derives from PageBase and uses a master page. 从PageBase派生并使用母版页的asp.net页。
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="lblContent" runat="server" Text="<%# this.TestString %>" />
</asp:Content>
You need to call the DataBind()
method. 您需要调用
DataBind()
方法。
In page_load handler, 在page_load处理程序中,
TestString="Testing a property";
lblContent.DataBind();
//or
DataBind();
EDIT 编辑
The code below doesn't actually work—I tested it, just not well. 下面的代码实际上并不起作用-我对其进行了测试,只是效果不佳。 What OP was really looking for was:
OP真正想要的是:
<div id="lblContent"><%= this.TestString %></div>
I think you need to change this 我认为你需要改变这个
<asp:Label ID="lblContent" runat="server" Text="<%# this.TestString %>" />
to this 对此
<asp:Label ID="lblContent" runat="server" Text="<%= this.TestString %>" />
<%# just runs code, but doesn't output anything. <%#仅运行代码,但不输出任何内容。 <%= will output whatever is inside those tags.
<%=将输出这些标记内的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.