如何动态更改母版页的母版页?
我正在尝试动态更改母版页,尽管从内容页(覆盖 OnPreInit
)很容易做到,但母版页没有此类事件。是否可以以某种方式介绍这个活动?
更新:我通过梯子底部页面的PreInit
到达了一半,事实证明你可以做base.Master.MasterPageFile之类的事情= "/master.Master";
,但由于某种原因,这不会加载最顶层母版页标题中的内容,即样式表。
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
引用自: 我可以动态更改嵌套母版页的母版吗?< /a>
您需要确保 ContentPlaceholderIds 在您交换的页面之间保持一致。
Quoting from: Can I change a nested master page's master dynamically?
Obviously you will need to ensure that the ContentPlaceholderIds are consistent across the pages you are swapping between.
如果您覆盖 MasterPageClass 并添加您自己的 onPreInit 您可能可以做到这一点,但我认为即使这样也行不通。根据 Reflector 的说法,它绝对没有构造,甚至没有什么可以覆盖的,尽管因为它继承了 UserControl 那么总是有 OnInit ...或者你可以尝试覆盖 get_Master() 但这可能也不起作用...
If you overrode the MasterPageClass and added your own onPreInit you might could do it, but I don't think even that would work. There's definitely no construct for it according to Reflector, nothing to even override, altho since it inherits UserControl then there's always OnInit ... alternately you could attempt to override get_Master() but that might not work either ...
使用母版页构造函数。
Use the masterpage constructor.
假设您想使用不带菜单的不同母版页,请传递查询字符串 NoMenu。
Let's say you want to use a different master page without a menu, pass query string NoMenu.