65.9K
CodeProject 正在变化。 阅读更多。
Home

继承和嵌套母版页

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.69/5 (6投票s)

2007年5月3日

1分钟阅读

viewsIcon

77101

downloadIcon

530

这篇文章介绍了如何从.aspx代码中编辑顶级母版页的继承属性。

引言

这篇文章讲述了我尝试使用继承的嵌套母版页时遇到的一个小问题。如果您正在寻找关于母版页的通用信息,请参阅这篇文章 母版页内部

问题描述

好的,这是我的情况:

Screenshot - NestedMasterPages.gif

如您所见,我尝试从Main.Master继承Child.Master,然后在MyPage中使用Child.Master。

Main.master

<%@ Master Language="C#" AutoEventWireup="true" 
    CodeBehind="Main.master.cs" Inherits="Master.Main" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head id="mainHead" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="mainForm" runat="server">
        <asp:Literal ID="TopMessageLiteral" runat="server" />
        
        <div id="content">           
            <asp:contentplaceholder id="cphMain" runat="server" />
    </div>
    </form>
</body>
</html>

Child.master

<%@ Master Language="C#" MasterPageFile="~/Master/Main.master" AutoEventWireup="true" 
    CodeBehind="Child.master.cs" Inherits="Master.Child" %>
<%@ MasterType VirtualPath="~/Master/Main.master" %>
<asp:Content ID="cntMain" ContentPlaceHolderID="cphMain" Runat="Server">
    <div id="left">       
        <asp:contentplaceholder id="cphLeftCol" runat="server" />
    </div>  
    
    <div id="right">
        <asp:contentplaceholder id="cphRightCol" runat="server" />    
    </div>
</asp:Content>

MyPage

<%@ Page Language="C#" MasterPageFile="~/Master/Child.master" AutoEventWireup="true" 
    Inherits="MyPage" Title="Untitled Page" Codebehind="MyPage.aspx.cs" %>
<%@ MasterType VirtualPath="~/Master/Child.master" %>

<asp:Content ID="cntLeftCol" ContentPlaceHolderID="cphLeftCol" Runat="Server">
    <p>test left</p>
</asp:Content>
<asp:Content ID="cntRightCol" ContentPlaceHolderID="cphRightCol" Runat="Server">
    <p>test right</p>
</asp:Content>

请参阅下面的代码:

//Main.master
//-------------
public partial class Main : System.Web.UI.MasterPage {
  
  private string _topMessage;

  public string TopMessage {
    get { return _topMessage; }
    set { _topMessage = value; }
  }

  protected void Page_Load(object sender, EventArgs e) {
    TopMessageLiteral.Text = TopMessage;
  }
}

//Child.master 
//------------
public partial class Child : Main {

   protected new void Page_Load(object sender, EventArgs e) {
               
   }

}

//MyPage.aspx 
//----------
public partial class MyPage: System.Web.UI.Page {

  protected void Page_Load(object sender, EventArgs e) {
    Master.TopMessage = "My top message";
  }
}
这段代码看起来不错,但它不起作用。我的TopMessage永远不会显示。如果您尝试调试这段代码,您会看到当您在MyPage.Page_Load中实例化TopMessage属性时,一切都进展顺利,并且值已成功保存到_topMessage变量中。但是,当它从Main.Page_Load读取时,它是空的。坦率地说,我不知道为什么。我只有一些猜测。但我似乎找到了解决方案。

解决方案

诀窍是使用Context.Items集合。
//Main.master
//-------------
public partial class Main : System.Web.UI.MasterPage {
  
  public string TopMessage {
    get {
       if (Context.Items["_topMessage"] != null)
          return Context.Items["_topMessage"].ToString();
       else
          return string.Empty;
    }
    set {
        Context.Items["_topMessage"] = value;
    }
  }

  protected void Page_Load(object sender, EventArgs e) {
    TopMessageLiteral.Text = TopMessage;
  }
}
这样,TopMessage值将永远不会丢失。

另一种解决方案

Irwan Hassan建议的技术不需要Main.master <- Child.master继承

//MyPage.aspx 
//----------
public partial class MyPage: System.Web.UI.Page {

  protected void Page_Load(object sender, EventArgs e) {
    Master.Master.TopMessage = "My top message";
  }
}

历史

2007年5月9日:添加一段代码进行实验

2007年5月10日:添加另一个解决方案

© . All rights reserved.