使用嵌套母版页

发布于 2024-07-12 06:41:40 字数 2167 浏览 5 评论 0原文

我对 ASP.NET 很陌生,请帮助我更多地了解 MasterPages 概念。

我有 Site.master ,其中包含常见的标题数据(css、元等)、中心表单(空白)和页脚(版权信息、联系我们链接等)。

<%@ 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>

我想将第二个母版页用于子目录中的项目,其中包含 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>

但我有一个问题:页脚不显示。

我的错误在哪里? 我使用第二个母版页作为日志记录的超类是否正确?

项目页面如下所示:

<%@ 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'm very new to ASP.NET, help me please understand MasterPages conception more.

I have Site.master with common header data (css, meta, etc), center form (blank) and footer (copyright info, contact us link, etc).

<%@ 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).

<%@ 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

糖果控 2024-07-19 06:41:40

我一直在使用嵌套母版页并遇到类似的情况。 据我所知,Site.Master 中的“此处有一些页脚”是问题所在,我在 contentplaceholder 标记中包含内容时也遇到了类似的问题。 如果您尝试这样做

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

那么您应该能够看到页脚内容。

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. if you try this instead

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

Then you should be able to see the footer content.

栖迟 2024-07-19 06:41:40

我不确定我是否会为此使用母版页。 如果真的只是要进行日志记录,我会实现 IHttpModule,将其注册到 web.config 中,然后根据请求的路径检查是否进行日志记录。 我认为母版页是关于内容的,而不是其他处理(例如日志记录)。

有关示例,请参阅 MSDN 上的 IHttpModule 演练 - 在 BeginRequest 处理程序中,您可能会检查请求路径并适当记录(如果匹配)。

如果我误解了你想要做什么,我很抱歉。

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. 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.

Apologies if I misunderstood what you're trying to do though.

我的痛♀有谁懂 2024-07-19 06:41:40

您应该将 ContentPlaceHolder 留空,因为它会被实际页面中的内容内容所取代...

当您将“此处的一些页脚”文本移动到您的内容时,您将看到您的文本行:)

HTH

You should leave your ContentPlaceHolder empty, for it gets substituted by the content of the Content in your actual Page...

When you move the "Some footer here" text to your Content, you will see your lines of text :)

HTH

爱给你人给你 2024-07-19 06:41:40

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

问题是您在这种情况下使用子母版页是正确的 - 我会说母版页应该帮助您解决有关构建一致布局的问题,而不是是否应该进行日志记录的问题。

This link gives a simple explanation on Master pages,
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.

平生欢 2024-07-19 06:41:40

问题是,当放置在 Default.aspx 中的文本元素放入其相对的内容占位符中时,它们会写在 Site.master 页面的占位符上,而不是 Project.master 页面的占位符上(它们具有相同的名称)。

您应该通过将不同的 ContentPlaceHolderID 分配给 Project.master 中的占位符来解决命名冲突(这意味着您还必须更改 Default.aspx 中的引用)。

这将是您的 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>

因此,使用 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>

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).

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).

This would be your Project.master file:

<%@ 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:

<%@ 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>
夜夜流光相皎洁 2024-07-19 06:41:40

如果唯一的原因是实现登录,为什么要乱搞母版页呢?
如果日志记录不应该显示任何文本!?

你要么按照 Skeet 的建议使用 IHTTP 处理程序。或者更懒的方法是拥有一个从网页派生的类,并在该类中实现日志记录,并使你的页面需要来自该类的日志记录服务。

例如:

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

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

If the only reason is to implement loggin why would you mess around with 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..

ex:

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

partial class OneOfTheWebPages : LoggingPage
{
 public void onLoad()
{
base.onLoad();
}
}
递刀给你 2024-07-19 06:41:40

我可能误解了您的问题 - 但从您发布的代码来看,页脚中没有任何内容。

在您的项目页面中,holderFooter 内容占位符的 标记中没有任何内容。

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.

牵你手 2024-07-19 06:41:40

我有下一个继承树:

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

我不知道为什么 Page2 仅显示其自身及其母版页 - 项目的内容。 但不显示 Site 的内容(如 Page1 所示),为什么? 我要写什么才能做到这一点?

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. But doesn't display a content of Site (as Page1 does) Why? What have I to write for doing that?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文