问题描述
如何通过母版页调用内容页的方法
how to call a method of content page through master page
推荐答案
(ContentPlaceholderPageName.Page as PageBase).YourMethod();
ContentPlaceholderPageName是母版页中ContentPlaceHolder的ID. PageBase是包含YourMethod方法的基类.
ContentPlaceholderPageNameis the ID of the ContentPlaceHolder in your master page. PageBase is the base class containing the YourMethod method.
Page pg = ContentPlaceholderPageName.Page as Page;
if(pg != null)
{
pg.Whatever();
}
但我严重不建议这样做.每当我们看到母版页引用内容页的情况时,我们都应该重新考虑我们的设计.始终使内容页面引用母版页.在母版页中具有可将数据推送到其中的属性.原因是母版页不应依赖于内容页,因为会有许多使用母版页的内容页.反向依赖关系还可以,实际上建议用于任何母版页通信.
but i would seriously not recommend that. whenever we see a scenario where master page is referring the content page, we should rethink our design. Always make content page refer to master page. have properties in master page to push data in it. the reason is that the master page should not depend on content page because there will be many content pages using the master page. the reverse dependency is OK and in fact recommended for any master-content page communication.
这篇关于母版页和内容页的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!