可能重复:
content page class method calling from master page class
我需要从主页面事件中访问内容页面方法。我怎么能这样做?
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 ???
}
}
答案 0 :(得分:11)
此答案取自Interacting with the Content Page from the Master Page
您可以使用代表执行此操作。
例如,您在MasterPage中有一个按钮,并且您想要从母版页调用内容页面中的方法。这是母版页中的代码。
母版页:
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;
}
内容页面:
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);
}
}
并添加以下行在VirtualPath中指定MasterPage路径
<%@ MasterType VirtualPath="~/MasterPage.master" %>
// This is Strongly Typed Reference