Calling Content Page Method from MasterPage Method [duplicate]

拜拜、爱过 提交于 2019-11-29 13:06:16
Krishna Thota

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