簡體   English   中英

如何從asp.net C#的母版頁訪問內容頁控件?

[英]How to access content page control from master page in asp.net c#?

我的default.aspx中有一個文字控件,我想從我的主頁訪問它。當我嘗試執行此操作時,出現了空異常錯誤(缺少對象引用)。

您可以在母版頁中使用FindControl

內容頁:

<asp:Literal runat="server" ID="myLiteral"></asp:Literal>

並在您的母版頁加載(或其他位置)中:

protected void Page_Load(object sender, EventArgs e) {
    var myLiteral = ContentPlaceHolder1.FindControl("myLiteral");
    if (myLiteral != null) {
        ((Literal)myLiteral).Text = "Hello World!";
    }
}

其中ContentPlaceHolder1是母版頁中占位符的ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM