主页面中的调用方法

我的asp.net母版页中有一个公共方法。 是否可以从内容页面调用它,如果是,那么步骤/语法是什么?

Page内,您可以将主页面转换为特定类型(您公开所需function的Master类型),使用类型不匹配的任何例外:

 var master = Master as MyMasterPage; if (master != null) { master.Method(); } 

在上面的代码中,如果Master不是MyMasterPage类型,那么master将为null并且不会尝试任何方法调用; 否则将按预期调用。

使用MasterType指令 ,例如:

 <%@ MasterType VirtualPath="~/masters/SourcePage.master" %> 

然后你可以使用这样的方法:

 Master.Method(); 

你可以简单地做…

 MasterPageClassName MasterPage = (MasterPageClassName)Page.Master; MasterPage.MasterMethod(); 

检查详细信息访问具有代码的主页中的方法

 MyMasterPageType master = (MyMasterPageType)this.Master; master.MasterPageMethod();