ASP.NET:通过用户控件在母版调用函数 [英] ASP.NET: Call function in MasterPage through UserControl

查看:127
本文介绍了ASP.NET:通过用户控件在母版调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个页面中调用从母版功能是相当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:


  1. 创建,其中包括你的方法的接口。

  2. 实现该接口在你的母版页

  3. 从你的控制,参考 this.Page.Master 通过接口类型。

  4. 打电话给你的方法。

  1. Create an interface that includes your method.
  2. Implement the interface in your master page
  3. From your control, reference this.Page.Master via the interface type.
  4. Call your method.

这是一个更好的面向对象方法,导致少耦合,肯定将比运行时反射更好的表现。

This is a better OO approach, leads to less coupling, and will certainly perform better than runtime reflection.

我希望这有助于!

这篇关于ASP.NET:通过用户控件在母版调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆