-1

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  ???
    }
}
Community
  • 1
  • 1
Krishna Thota
  • 6,646
  • 14
  • 54
  • 79
  • @Habib I know It is a Duplicate but I found this Answer after lot of effort. So I posted it so that someone needs It. – Krishna Thota Nov 29 '12 at 07:04
  • I am surprised that you found it after a lot of time, Its the 2nd result on [Google Search](https://www.google.com/#hl=en&tbo=d&output=search&sclient=psy-ab&q=Call+method+of+Content+page+from+master+page&oq=Call+method+of+Content+page+from+master+page&gs_l=hp.3..0i30j0i22.766.766.0.1369.1.1.0.0.0.0.303.303.3-1.1.0.les%3B..0.0...1c.1.CY-4G7FWlKQ&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&fp=67a221e9661370f7&bpcl=38897761&biw=1280&bih=923) if you search by `Call method of Content page from master page` – Habib Nov 29 '12 at 07:06
  • 1
    And the [first result](https://www.google.com/#hl=en&tbo=d&sclient=psy-ab&q=Calling+Content+Page+Method+from+MasterPage+Method&oq=Calling+Content+Page+Method+from+MasterPage+Method&gs_l=hp.3...74225.74225.1.75008.1.1.0.0.0.0.239.239.2-1.1.0.les%3B..0.0...1c.1.bR_AaQS8mFU&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&fp=67a221e9661370f7&bpcl=38897761&biw=1280&bih=923) if you search by title of your question – Habib Nov 29 '12 at 07:07
  • I saw that post and I made my requirement work using the Link from that answer. That is why I have posted the Link In my answer. – Krishna Thota Nov 29 '12 at 07:12
  • you can go this [question](http://stackoverflow.com/questions/8165930/how-to-call-content-page-function-from-master-page) and post your answer there with the code, but currently IMO your question is duplicate to existing questions on SO – Habib Nov 29 '12 at 07:17

1 Answers1

12

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
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Krishna Thota
  • 6,646
  • 14
  • 54
  • 79