简体   繁体   English

使用嵌套的母版页

[英]Using nested Master Pages

I'm very new to ASP.NET, help me please understand MasterPages conception more. 我是ASP.NET的新手,请帮助我更多地了解MasterPages概念。

I have Site.master with common header data (css, meta, etc), center form (blank) and footer (copyright info, contact us link, etc). 我有Site.master有常见的标题数据(css,meta等),中心表单(空白)和页脚(版权信息,联系我们链接等)。

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="_SiteMaster" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="tagHead" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
    <form id="frmMaster" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="holderForm" runat="server"></asp:ContentPlaceHolder>
        <asp:ContentPlaceHolder ID="holderFooter" runat="server">Some footer here</asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

and I want to use second master page for a project into sub directory, which would contains SQL query on Page_Load for logging (it isn't necessary for whole site). 并且我想将项目的第二个母版页用于子目录,该目录将包含用于记录的Page_Load上的SQL查询(整个站点不需要)。

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
    <asp:ContentPlaceHolder ID="holderForm" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
    <asp:ContentPlaceHolder ID="holderFooter" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>

But I have a problem: footer isn't displayed. 但我有一个问题:没有显示页脚。

Where is my mistake? 我的错误在哪里? Am I right to use second master page as super class for logging? 我是否正确使用第二个母版页作为记录的超级类?

Project page looks like this: 项目页面如下所示:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
    <p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="Server">
    Some footer content
</asp:Content>

I've been working with nested master pages and have run in to something similar. 我一直在使用嵌套的母版页并运行类似的东西。 From what I see where you have "Some footer here" in the Site.Master is where the problem lies and I've had similar problems with having content with-in a contentplaceholder tag. 从我看到你在Site.Master中有“一些页脚”的地方,问题所在,我在内容持有人标签中有内容。 if you try this instead 如果你试试这个

<asp:ContentPlaceHolder ID="holderFooter" runat="server"/>Some footer here

Then you should be able to see the footer content. 然后你应该能够看到页脚内容。

I'm not sure I'd use master pages for this. 我不确定我是否会使用母版页。 If it's really just going to do logging, I'd implement IHttpModule, register it in web.config, and then check whether or not to log based on the path of the request. 如果它真的只是要进行日志记录,我将实现IHttpModule,在web.config中注册它,然后检查是否根据请求的路径进行日志记录。 I think of master pages as being about content rather than other processing such as logging. 我认为母版页是关于内容而不是其他处理,例如日志记录。

See the IHttpModule walkthrough on MSDN for an example - in your BeginRequest handler, you'd probably check the request path and log appropriately if it matched. 有关示例,请参阅MSDN上的IHttpModule演练 - 在BeginRequest处理程序中,您可能会检查请求路径并在匹配时正确记录。

Apologies if I misunderstood what you're trying to do though. 如果我误解了你想要做的事,我会道歉。

You should leave your ContentPlaceHolder empty, for it gets substituted by the content of the Content in your actual Page... 您应该将ContentPlaceHolder留空,因为它会被您实际页面中内容的内容所取代...

When you move the "Some footer here" text to your Content, you will see your lines of text :) 当您将“Some footer here”文本移动到您的内容时,您将看到您的文本行:)

HTH HTH

This link gives a simple explanation on Master pages, http://waxtadpole.wordpress.com/2009/01/16/master-page-content-not-visible-visual-studio-2008/ 此链接在母版页上提供了简单的说明, http://waxtadpole.wordpress.com/2009/01/16/master-page-content-not-visible-visual-studio-2008/

The question are you right to use child Master pages in this instance - I would say master pages should be helping you solve issues around building a consistent layout, not for whether or not logging should occur. 问题是你在这个例子中使用子母版页是正确的 - 我会说母版页应该帮助你解决建立一致布局的问题,而不是是否应该进行日志记录。

The problem is, when the text elements placed inside Default.aspx are put in their relative Content Placeholders, they are written on the placeholders of your Site.master page and not those of Project.master (which have the same names). 问题是,当Default.aspx中放置的文本元素放在它们的相对内容占位符中时,它们将写在Site.master页面的占位符上,而不是Project.master(具有相同名称)的占位符。

You should resolve the naming conflict, by assigning different ContentPlaceHolderIDs to the the placeholders in Project.master (this means you'll also have to change the references in Default.aspx). 您应该通过将不同的ContentPlaceHolderID分配给Project.master中的占位符来解决命名冲突(这意味着您还必须更改Default.aspx中的引用)。

This would be your Project.master file: 这将是您的Project.master文件:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
    <!-- whatever... -->
    <asp:ContentPlaceHolder ID="holderFormInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
    <!-- ... -->
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
    <asp:ContentPlaceHolder ID="holderFooterInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>

And thus, your .aspx pages that use the Project master page instead of the global Page.master must be changed to: 因此,使用Project母版页而不是全局Page.master的.aspx页面必须更改为:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderFormInternal" runat="server">
    <p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooterInternal" runat="Server">
</asp:Content>

If the only reason is to implement loggin why would you mess around with masterpages? 如果唯一的原因是实现loggin为什么你会搞乱masterpages呢? If the logging isent supposed to display any text!? 如果日志记录应该显示任何文本!?

You either do as Skeet proposed with an IHTTP handler.. Or lazier one would be do have a class that derives from webpage and implement logging in that class and make your pages that need logging dervice from that.. 你可以像Skeet提出的那样使用IHTTP处理程序。或者更懒的人会有一个派生自网页的类,并在该类中实现日志记录,并使你的页面需要从那里进行日志记录。

ex: 例如:

public class LoggingPage : : System.Web.UI.Page
{
  public override void OnLoad()
{
// Do logging
}
}

partial class OneOfTheWebPages : LoggingPage
{
 public void onLoad()
{
base.onLoad();
}
}

I may be misunderstanding your problem - but from the code you've posted, there isn't anything in the footer. 我可能会误解你的问题-但是从您发布的代码,没有页脚任何东西。

In your Project page, the <asp:Content> tag for the holderFooter content place holder doesn't have anything in it. 在Project页面中, holderFooter内容占位符的<asp:Content>标记中没有任何内容。

I have next inheritance tree: 我有下一个继承树:

Site.master <-- Page1.aspx
<-- Project.master <-- Page2.aspx

And I don't know why Page2 display only content of itself and it's master page - Project. 我不知道为什么Page2只显示自己的内容和它的主页 - 项目。 But doesn't display a content of Site (as Page1 does) Why? 但不显示网站的内容(如Page1所做)为什么? What have I to write for doing that? 我有什么要写的呢?

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

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