You can use an event
that your MasterPage offers to communicate with the Page.
Within your MasterPage
create something like these
public event EventHandler Button1ClickEvent;
public event EventHandler Button2ClickEvent;
What is going on here is that you are creating an event that the MasterPage can check for a page that is listening for and fire an event to that page to signal that is has been called. In this case EventHandler
means that you'll be passing EventArgs
to the event listener.
You send an event by doing this
protected void Button1_Click(object sender, EventArgs e)
{
if (Button1ClickEvent != null) // check if a sub page is implementing it
Button1ClickEvent(this, EventArgs.Empty); // fire the event off
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Button2ClickEvent != null)
Button2ClickEvent(this, EventArgs.Empty);
}
Next, wire up your Page
to implement these events
Add in this to your Page definition
<%@ MasterType VirtualPath="~/MasterPage.master" %>
Now you can access these events in your page code-behind
private void Master_Button1Click(object sender, EventArgs e)
{
// This is called when the `Button1ClickEvent` is fired from your MasterPage
}
private void Master_Button2Click(object sender, EventArgs e)
{
// This is called when the `Button2ClickEvent` is fired from your MasterPage
}
protected void OnInit(EventArgs e)
{
// Setup an event handler for the buttons
Master.Button1ClickEvent += new EventHandler(Master_Button1Click);
Master.Button2ClickEvent += new EventHandler(Master_Button2Click);
}