从MasterPage方法调用内容页面方法
可能重复:
从母版页类调用的内容页面类方法
我需要从主页面事件中访问内容页面方法。 我怎样才能做到这一点?
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 ??? } }
此答案取自与母版页中的内容页面交互
您可以使用Delegates执行此操作。
例如,您在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