Search code examples
c#asp.netmaster-pagescontent-pages

Calling Content Page Method from MasterPage Method


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

Solution

  • 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