简体   繁体   English

嵌套母版页以编程方式更改

[英]Nested Master Pages change programmatically

I have a PAGE with a structure like this: 我有一个具有这样结构的PAGE:

PAGE = MASTER PAGE A + nested MASTER PAGE B of A

MASTER PAGE A: 主页面A:

-----------
Header
-----------
BODY
-----------
Footer
-----------

MASTER PAGE B: 主页面B:

BODY-------------------------------
          |         |             |  
ColumLeft | Content | ColumRight  |
          |         |             |
-----------------------------------

One of the features I need to develop is to be able to change the nested MASTER PAGE programmatically. 我需要开发的功能之一是能够以编程方式更改嵌套的MASTER PAGE。 For example changing MASTER PAGE B with C (containing a different layout like just 2 columns) maintaining the Header and Footer centralized. 例如,用C更改MASTER PAGE B(包含不同的布局,如2列),以保持Header和Footer集中。

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. 目前在PAGE上,我使用此代码来选择另一个MP,但是我无法做到这一点,因为似乎在选择新的嵌套页面asp.net时,它失去了对主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? 最好只拥有一个母版页(1级),并在页眉和页脚中添加另一个构造? If yes what would you suggest me? 如果是,您会建议我什么?

In this case I wouldn't use nested masterpages, but just one masterpage. 在这种情况下,我不会使用嵌套的母版页,而只会使用一个母版页。 For the columns I would use RenderPartial or RenderAction. 对于列,我将使用RenderPartial或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. 它不像您希望的那样干燥,因为您需要在每个视图中添加RenderPartial(“ LeftColumn”),因此我了解您的问题,但这就是我的方法。

NB: RenderAction is availbale in MVC. 注意:RenderAction在MVC中可用。 For an article about it see eg: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx 有关其的文章,请参见: http : //haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

In order to change the MasterPage programmatically you have to specify its type right after the page registration link in the xml/html page. 为了以编程方式更改MasterPage,您必须在xml / html页面中的页面注册链接后立即指定其类型。

<%@ Page Language="C#" 
    MasterPageFile="~/MasterPage.master" 
    AutoEventWireup="false" 
    CodeFile="MyCodeFile.aspx.cs" 
    Inherits="MyCodeFile"
    title="Untitled Page" %>
<%@ MasterType 
    virtualpath="~/MasterPage.master" 
%>

I don't recommend using this architecture to achieve what you want to achieve, but this is how you would do it. 我不建议使用此体系结构来实现您想要实现的目标,但这就是您将要做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM