我的内容页类中有一个公共方法,我想从母版页类中调用此方法
谢谢
发布于 2009-05-20 10:35:09
您可以从基类继承页面。然后,您可以在您的基类中创建一个虚拟方法,该方法将在您的页面中被覆盖。然后您可以从母版页调用该虚拟方法,如下所示-
(cphPage.Page as PageBase).YourMethod();这里,cphPage是母版页中ContentPlaceHolder的ID。PageBase是包含YourMethod方法的基类。
EDIT:当然,在使用页面的实例调用YourMethod方法之前,必须进行null检查。
发布于 2009-05-20 10:53:08
如果您不想使用任何基页
将此添加到您的母版页中,
private object callContentFunction(string methodName, params object[] parameters)
{
Type contentType = this.Page.GetType();
System.Reflection.MethodInfo mi = contentType.GetMethod(methodName);
if(mi == null)return null;
return mi.Invoke(this.Page, parameters);
}然后使用它
callContentFunction("myPublicMethodName",myParam1,myParam2...);
发布于 2012-05-30 17:56:52
步骤:
<%@ MasterType VirtualPath="location of your masterpage" %>指令添加到.aspx页Master.functionName().在内容页中声明一个公共函数
https://stackoverflow.com/questions/887178
复制相似问题