Dot-Net

ASP.NET - 從內容頁面訪問母版頁元素

  • December 23, 2020

可以從內容頁面訪問母版頁的元素嗎?

假設我有從 MasterPage1 繼承的 MasterPage1 和 ContentPage1,而 MasterPage1 有一個按鈕:Button1。

我可以從內容頁面更改該按鈕的屬性,例如使 Button1 不可見、不活動等嗎?我怎樣才能做到這一點?

我正在使用.net2.0

您必須在頁面/使用者控制項標記中引用 MasterPage。

<%@ Reference VirtualPath="..." %>

然後在程式碼隱藏中,您只需將 Page.MasterPage 轉換為您的 MasterPage 並訪問其屬性。

MyMasterPage myMasterPage = (MyMasterPage)Page.Master;

是的…如果您需要使用 MasterPage 從 aspx 頁面執行此操作,它將是:

Button myButton = (Button)Master.FindControl("myButton");
myButton.Visible = false;

引用自:https://stackoverflow.com/questions/461911