嵌套母版页以编程方式更改
我有一个具有如下结构的页面:
PAGE = MASTER PAGE A + nested MASTER PAGE B of A
MASTER PAGE A:
-----------
Header
-----------
BODY
-----------
Footer
-----------
MASTER PAGE B:
BODY-------------------------------
| | |
ColumLeft | Content | ColumRight |
| | |
-----------------------------------
我需要开发的功能之一是能够以编程方式更改嵌套的 MASTER PAGE。 例如,将母版页 B 更改为 C(包含不同的布局,例如仅 2 列),保持页眉和页脚集中。
目前,在页面上,我使用此代码来选择另一个 MP,但我无法执行此操作,因为在选择新的嵌套页面时,asp.net 似乎松散了对主页面 A 的引用。
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
问题:
- 知道如何解决吗它?
- 只有一个母版页(1 级)并包含页眉和页脚以及其他技术会更好吗?如果是的话你会建议我什么?
I have a PAGE with a structure like this:
PAGE = MASTER PAGE A + nested MASTER PAGE B of A
MASTER PAGE A:
-----------
Header
-----------
BODY
-----------
Footer
-----------
MASTER PAGE B:
BODY-------------------------------
| | |
ColumLeft | Content | ColumRight |
| | |
-----------------------------------
One of the features I need to develop is to be able to change the nested MASTER PAGE programmatically.
For example changing MASTER PAGE B with C (containing a different layout like just 2 columns) maintaining the Header and Footer centralized.
At the moment on PAGE I use this code to select another MP, but I am not able to do it because seems when selecting a new nested page asp.net loose the reference to the main MASTER PAGE A.
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
Questions:
- Any idea how to solve it?
- Would be better have only a Master Page (1 Level) and include the Header and Footer with another tecnic? If yes what would you suggest me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了以编程方式更改 MasterPage,您必须在 xml/html 页面中的页面注册链接之后指定其类型。
我不建议使用这种架构来实现您想要实现的目标,但这就是您要做的。
In order to change the MasterPage programmatically you have to specify its type right after the page registration link in the xml/html page.
I don't recommend using this architecture to achieve what you want to achieve, but this is how you would do it.
在这种情况下,我不会使用嵌套母版页,而只使用一个母版页。对于列,我将使用 RenderPartial 或 RenderAction。它并不像您希望的那样干燥,因为您需要在每个视图中添加 RenderPartial("LeftColumn") ,所以我理解您的问题,但这就是我这样做的方式。
注意:RenderAction 在 MVC 中可用。有关它的文章,请参阅例如:
http://haacked.com/archive/2009/11/18 /aspnetmvc2-render-action.aspx
In this case I wouldn't use nested masterpages, but just one masterpage. For the columns I would use RenderPartial or RenderAction. It's not as DRY as you would wish, because you need to add RenderPartial("LeftColumn") in every view, so I understand your problem, but this is the way I do it.
NB: RenderAction is availbale in MVC. For an article about it see e.g.:
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx