我在asp.net内容页面中有一个公共方法.是否可以从母版页调用此方法 [英] I have a public method in my asp.net content Page. Is it possible to call this from a Master page

查看:67
本文介绍了我在asp.net内容页面中有一个公共方法.是否可以从母版页调用此方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



I have a public method in my asp.net content Page. Is it possible to call this from a Master page and if so what are the steps/syntax?

推荐答案



您可以参考此链接

http://odetocode.com/Articles/450.aspx [
谢谢
Vinod
Hi,

You can refer this link

http://odetocode.com/Articles/450.aspx[^]

Got to section : "Master Page To Content Page Interaction"

Thanks
Vinod


可以,但这并不那么简单.
考虑一下:这个想法是有一个母版页,并且使用它有许多不同的内容页.编写母版"页面时,您不知道将要引用哪个内容"页面.因此,您不能直接调用该方法,因为它在运行时可能不存在于内容"页面中-它可能是一个完全不同的页面,没有定义该方法.

话虽如此,您可以从基类继承页面.然后,您可以在基类中创建一个虚拟方法,该方法将在您的页面中被覆盖.然后,您可以从母版页调用该虚拟方法:
You can, but it''s not as simple as that.
Think about it: The idea is that there is a single master page, and a number of different content pages using it. When you wrote the Master page, you do not know which Content page you are going to be referring to. So, you can''t just call the method directly, as it may not exist in the Content page at run time - it could be a completely different page which does not define the method.

Having said that, you can inherit your page from a base class. Then you can create a virtual method in your base class which will get overridden in your page. You can then call that virtual method from the master page:
BasePage bp = myPage.Page as BasePage;
if (bp!= null)
   {
   bp.MyMethod();
   }


这篇关于我在asp.net内容页面中有一个公共方法.是否可以从母版页调用此方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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