为什么我的 ASP.NET 母版页不能在内容页中编译?

发布于 2024-08-06 01:49:51 字数 2323 浏览 1 评论 0原文

我正在编写第一个包含母版页的 ASP.NET Web 应用程序。然而,即使我似乎按照书本使用它们,但在运行项目时母版页似乎不起作用。

我创建了一个 Default.MasterPage lite:

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

<!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 runat="server">
    <title>My new pager</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
            <tr>
                <asp:ContentPlaceHolder ID="PageHeader" runat="server">   
                </asp:ContentPlaceHolder>
            </tr>
            <tr>
            <asp:ContentPlaceHolder ID="Navigation" runat="server">
            </asp:ContentPlaceHolder>
            </tr>
            <tr>
                <asp:ContentPlaceHolder ID="Main" runat="server">  
                </asp:ContentPlaceHolder>
            </tr>
            <tr>
                <asp:ContentPlaceHolder ID="Footer" runat="server">  
                </asp:ContentPlaceHolder>
            </tr>      
            </table>     
        </div>
    </form>
</body>
</html>

和两个这样的内容页面: (PageHeader.aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageHeader.aspx.cs" Inherits="TimeTracker.PageHeader"  MasterPageFile="~/Default.Master" Title="Header"%>

<asp:Content ID="Header" ContentPlaceHolderID="PageHeader" runat="server">
        Enalog Time-Tracker
</asp:Content>

和 (Default.aspx)

<%@ Page Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TimeTracker._Default" Title="Login"%>

<asp:Content ID="LoginPage" ContentPlaceHolderID="Main" runat="server">
Login page
</asp:Content>

但是在运行项目时,MasterPage 不会在内容页面中进行编译,即使我相信我已正确连接它们。因此,如果我运行 Default.aspx,我只会看到 ContentPlaceHolderID="Main" 中的内容,如果我运行 PageHeader.aspx,我只会看到 ContentPlaceHolderID="PageHeader" 中的内容。 有谁知道我为什么会出现这种行为,或者我在这里做错了什么? 提前致谢!

I'm writing my first ASP.NET web application containing Master Pages. However, even though I seem to use them by the book, when running the project the Master Pages don't seem to work.

I have created a Default.MasterPage lite this:

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

<!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 runat="server">
    <title>My new pager</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
            <tr>
                <asp:ContentPlaceHolder ID="PageHeader" runat="server">   
                </asp:ContentPlaceHolder>
            </tr>
            <tr>
            <asp:ContentPlaceHolder ID="Navigation" runat="server">
            </asp:ContentPlaceHolder>
            </tr>
            <tr>
                <asp:ContentPlaceHolder ID="Main" runat="server">  
                </asp:ContentPlaceHolder>
            </tr>
            <tr>
                <asp:ContentPlaceHolder ID="Footer" runat="server">  
                </asp:ContentPlaceHolder>
            </tr>      
            </table>     
        </div>
    </form>
</body>
</html>

And two content pages like this:
(PageHeader.aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageHeader.aspx.cs" Inherits="TimeTracker.PageHeader"  MasterPageFile="~/Default.Master" Title="Header"%>

<asp:Content ID="Header" ContentPlaceHolderID="PageHeader" runat="server">
        Enalog Time-Tracker
</asp:Content>

And (Default.aspx)

<%@ Page Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TimeTracker._Default" Title="Login"%>

<asp:Content ID="LoginPage" ContentPlaceHolderID="Main" runat="server">
Login page
</asp:Content>

But when running the project, the MasterPage don't compile in the content pages, even though I believe I've wired them up correctly. So if I for example runs Default.aspx, I only see is the content from the ContentPlaceHolderID="Main", and if I run PageHeader.aspx, I only see the content from the ContentPlaceHolderID="PageHeader".
Do anyone know why I get this behaviour, or perhaps what I'm doing wrong here?
Thanks in advance!

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

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

发布评论

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

评论(2

绝影如岚 2024-08-13 01:49:51

您需要将这两个部分都放在内容页面中,如下所示:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageHeader.aspx.cs"
    Inherits="TimeTracker._Default"  MasterPageFile="~/Default.Master" 
    Title="Login"%>

<asp:Content ID="HeaderContent" ContentPlaceHolderID="PageHeader" runat="server">
   This is my header
</asp:Content>

<asp:Content ID="MainContent" ContentPlaceHolderID="Main" runat="server">
   My main page content is here.
</asp:Content>

您可能不需要“标题”页面。

You need to put both sections in your content pages, like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageHeader.aspx.cs"
    Inherits="TimeTracker._Default"  MasterPageFile="~/Default.Master" 
    Title="Login"%>

<asp:Content ID="HeaderContent" ContentPlaceHolderID="PageHeader" runat="server">
   This is my header
</asp:Content>

<asp:Content ID="MainContent" ContentPlaceHolderID="Main" runat="server">
   My main page content is here.
</asp:Content>

You probably don't need the "Header" page.

浅唱ヾ落雨殇 2024-08-13 01:49:51

您的母版页不包含任何 标签,因此表格没有任何可显示的内容。

Your master page does not contain any <td> tags, so the tables do not have any displayable content.

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