问题
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