Calling Content Page Method from MasterPage Method [duplicate]

非 Y 不嫁゛ 提交于 2019-11-28 06:55:51

问题


Possible Duplicate:
content page class method calling from master page class

I need to access Content Page Method from Master page Event. How can I do this?

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

回答1:


This answer is taken from Interacting with the Content Page from the Master Page

You can do this using Delegates.

For Example, you have a button in MasterPage and you want to call a Method in Content Page from Master Page. Here is the Code in Master Page.

Master 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;
}

Content Page:

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

And Also add the Below Line Specifying you MasterPage Path in VirtualPath

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


来源:https://stackoverflow.com/questions/13620435/calling-content-page-method-from-masterpage-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!