如何动态更改母版页? [英] How to dynamically change master page's master page?

查看:64
本文介绍了如何动态更改母版页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态更改母版页,尽管从内容页面(覆盖 OnPreInit )很容易做到,但母版页没有此类事件.可以通过某种方式介绍此活动吗?

I am trying to change the master page dynamically, and although it's easy to do from a content page (overriding OnPreInit), there is no such event for a master page. Is it possible to introduce this event somehow?

更新:通过梯形图底部页面的 PreInit ,我到达了一半,事实证明您可以执行 base之类的事情.Master.MasterPageFile ="/master.Master"; ,但是由于某些原因,这不会将内容加载到最顶层母版页页眉(即样式表)中.

UPDATE: I got halfway there by going via the PreInit of the pages at the bottom of the ladder, turns out you can do things like base.Master.MasterPageFile = "/master.Master";, but for some reason this doesn't load the stuff in the header of the top-most master page, namely stylesheets.

推荐答案

引用自:

只需测试一下,它就可以在使用嵌套MasterPage的Page的PreInit中工作.受保护的void Page_PreInit(对象发送者,EventArgs e)
{
this.Master.MasterPageFile ="/Site2.Master";
}

Just tested this and it works from the PreInit of the Page that is using the nested MasterPage. protected void Page_PreInit(object sender, EventArgs e)
{
this.Master.MasterPageFile = "/Site2.Master";
}

显然,您需要确保 ContentPlaceholderId在要交换的页面之间保持一致.

这篇关于如何动态更改母版页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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