簡體   English   中英

asp.net c#中的訪問母版頁方法

[英]Access Master Page Method in asp.net c#

我應該如何從子頁面訪問母版頁的公共方法?

用戶大師.master.vb

 Public Sub UpdateCart()
 End Sub

默認.aspx.cs

如何從 Default.aspx.cs 頁面訪問UpdateCart()

從您的內容頁面,您可以使用它來實現要求並確保將其標記為不受保護的公共

VB

TryCast(Me.Master, MyMasterPage).UpdateCart()

C#

(this.Master as MyMasterPage).UpdateCart();

這樣做:

SiteMaster master = new SiteMaster();
//now call the master page method
master.test()

示例

//master page code behind
public partial class SiteMaster : System.Web.UI.MasterPage
{

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    //test method
    public void test()
    {
    }

}

//content page code behind
public partial class About : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        SiteMaster master = new SiteMaster();
        master.test();
    }

}

或者將SiteMaster方法SiteMaster static並直接調用它:

SiteMaster.MyStaticMethod()

暫無
暫無

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

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