2014-09-22 119 views
1

我有一个父母版(Main.master)和一个孩子NestedMasterPage分配母版页嵌套母版页的代码隐藏

在NestedMasterPage我已经分配的MasterPageFile属性在HTML代码中像下面

<%@ Master Language="C#" AutoEventWireup="true" MasterPageFile="~/Themes/ABC/Main.Master" CodeBehind="User_Main.master.cs" Inherits="ProjectABC.UI.Pages.MasterPages.User_Main" %> 

现在在NestedMasterPage后面的代码中,我正在尝试将MasterPageFile属性更改为其他MasterPage,但不工作。

protected void Page_PreInit(object sender, EventArgs e) 
{ 
    MasterPageFile = "~/Themes/XYZ/Main.Master"; 
} 

没有代码错误,只是HTML分配的路径正在工作,而不是后面的代码。我错过了什么?

回答

0

此问题背后的原因是主页面文件的PreInit()事件在asp.net页面生命周期中从不会被调用。您可以看到完整的主页和事件页面事件集here

您可以设置一个孩子的母版页的母版页的内容页面的下列方式PreInit()事件:

public partial class TestMaster : System.Web.UI.Page 
{ 
    protected void Page_PreInit(object sender, EventArgs e) 
    { 
     //Set the master page file of the current pages master. 
     this.Master.MasterPageFile = "~/NewMasterPage.master"; 
    } 
} 
+0

非常感谢。我几乎没有在解决这个问题上疯狂。你的帮助工作。非常感谢 – 2014-09-28 21:33:02

+0

请标记为答案,如果它帮助你。谢谢! – 2014-09-29 09:17:55