vb.net asp.net c#中的访问母版页方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19265135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:24:52  来源:igfitidea点击:

Access Master Page Method in asp.net c#

c#asp.netvb.net

提问by Harshit Tailor

How should I access public methods of master page from a child page?

我应该如何从子页面访问母版页的公共方法?

UserMaster.master.vb

用户大师.master.vb

 Public Sub UpdateCart()
 End Sub

Default.aspx.cs

默认.aspx.cs

How can I access UpdateCart()from the Default.aspx.cs page?

如何UpdateCart()从 Default.aspx.cs 页面访问?

回答by Pradip

From you Content page you can use this to achieve the requirement and make sure it marked as a publicnot protected:

从您的内容页面,您可以使用它来实现要求并确保将其标记为不受保护的公共

VB

VB

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

C#

C#

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

回答by Vinay Pratap Singh

Do it like this:

像这样做:

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

Example

例子

//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();
    }

}

回答by radders

Or make the SiteMastermethod staticand just call it directly:

或者制作SiteMaster方法static并直接调用它:

SiteMaster.MyStaticMethod()