ASP.NET:通过用户控件在母版调用函数 [英] ASP.NET: Call function in MasterPage through UserControl
问题描述
在一个页面中调用从母版功能是相当straigt前锋,但我怎么称呼它为用户控件:
Calling a function from the MasterPage in a Page is quite straigt forward but how do I call it for a UserControl:
添加<%@ VirtualPath的MasterType =〜/ MasterPage.master%GT;
,不会对用户控件工作。
Adding <%@ MasterType VirtualPath="~/MasterPage.master" %>
, doesn't work on UserControls.
所以 this.Page.Master.MyFunction()
失败:(
推荐答案
尼尔斯,
杠杆反射(如由JDunkerley建议的)是一种方法来解决问题。另外你可以考虑正在实施的接口:
Leverage reflection (as suggested by JDunkerley) is one approach to the problem. Another you might consider is implementing an interface:
- 创建,其中包括你的方法的接口。
- 实现该接口在你的母版页
- 从你的控制,参考
this.Page.Master
通过接口类型。 - 打电话给你的方法。
- Create an interface that includes your method.
- Implement the interface in your master page
- From your control, reference
this.Page.Master
via the interface type. - Call your method.
这是一个更好的面向对象方法,导致少耦合,肯定将比运行时反射更好的表现。
This is a better OO approach, leads to less coupling, and will certainly perform better than runtime reflection.
我希望这有助于!
这篇关于ASP.NET:通过用户控件在母版调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!