在內容頁中使用<%@ MasterType VirtualPath="母版頁url"%>指令之後,就可以直接用Master訪問母版頁的公共屬性。
如在母版頁中有個Label控件,Id爲LblTime,記錄當前時間。在母版頁中定義一個公共屬性
如:
<script runat="server">
public Label MasterPageLabel
{
get { return lblTime; }
set { lblTime = value; }
}
</script>
在內容頁中,調用<%@ MasterType VirtualPath="~/MasterPage.master" %>指令
<script runat="server">
public void Page_Load(object sender, EventArgs e)
{
Master.MasterPageLabel.Text = System.DateTime.Now.ToShortTimeString();
}
</script>
使用MasterType指令,就好像對母版頁進行了強類項轉換。