內容頁訪問母版頁屬性方法之二(MatrerType指令)

 在內容頁中使用<%@ 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指令,就好像對母版頁進行了強類項轉換。

發佈了27 篇原創文章 · 獲贊 4 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章