从调用方法母版页的内容方法[复制](Calling Content Page Method from

2019-07-04 06:20发布

可能重复:
内容页面类的方法从母版页类调用

我需要从母版页事件访问内容页方法。 我怎样才能做到这一点?

Content Page:
public partial class Call_Center_Main : System.Web.UI.Page
{
    Page_Load(object sender, EventArgs e)
    {
    }

    public void MenuClick(string ClkMenu)
    { 
     // Some Code
    }
}

MasterPage:
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
      //How Can I call MenuClick method from Content Page from Here  ???
    }
}

Answer 1:

这个答案是取自从母版页的内容页面交互

您可以使用委托做到这一点。

例如,你在母版一个按钮,你要调用从母版页的内容页面一个方法。 这是在母版页中的代码。

母版页:

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (contentCallEvent != null)
            contentCallEvent(this, EventArgs.Empty);
    }
    public event EventHandler contentCallEvent;
}

内容页:

public partial class Content_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void Master_ButtonClick(object sender, EventArgs e)
    {
        // This Method will be Called.
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        // Create an event handler for the master page's contentCallEvent event
        Master.contentCallEvent += new EventHandler(Master_ButtonClick);
    }
}

还可以添加以下行指定您在母版路径VirtualPath

<%@ MasterType VirtualPath="~/MasterPage.master" %> 
// This is Strongly Typed Reference


文章来源: Calling Content Page Method from MasterPage Method [duplicate]