我的asp.net母版页中有一个公共方法。可以从内容页面调用它吗?如果可以,步骤/语法是什么?
发布于 2011-06-13 23:57:52
在Page
中,您可以将Master
页面转换为特定类型(公开所需功能的您自己的Master
的类型),并使用as
避开任何类型不匹配的异常:
var master = Master as MyMasterPage;
if (master != null)
{
master.Method();
}
在上面的代码中,如果Master
不是MyMasterPage
类型,那么master
将为null
,并且不会尝试任何方法调用;否则将按预期进行调用。
发布于 2011-06-14 00:03:03
使用MasterType
directive,例如:
<%@ MasterType VirtualPath="~/masters/SourcePage.master" %>
然后,您可以使用如下方法:
Master.Method();
发布于 2011-06-13 23:58:39
你可以简单地这样做...
MasterPageClassName MasterPage = (MasterPageClassName)Page.Master;
MasterPage.MasterMethod();
https://stackoverflow.com/questions/6332889
复制相似问题