-1

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

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

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  ???
    }
}
4

1 回答 1

12

此答案取自Interacting with the Content Page from Master Page

您可以使用委托来执行此操作。

例如,您在 MasterPage 中有一个按钮,并且您想从 MasterPage 调用 Content Page 中的方法。这是母版页中的代码。

母版页:

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 中添加以下行指定您的 MasterPage 路径

<%@ MasterType VirtualPath="~/MasterPage.master" %> 
// This is Strongly Typed Reference
于 2012-11-29T07:00:07.967 回答