[英]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.