母版页怪异-“内容控件必须是内容页或引用母版页的嵌套母版页中的顶级控件。”

发布于 2024-07-19 07:16:12 字数 2327 浏览 2 评论 0原文

这很奇怪。 我在 Visual Studio 2008 中向我的解决方案添加了一个全新的 Web 应用程序项目。

创建了一个母版页。 进行了零修改。 创建了一个新的网络表单。 将其母版页设置为我刚刚创建的 MP。

尽管如此,没有任何修改。 没有标记。 没有用户控制。 没有参考文献。 没有什么。 但是,当我尝试运行它时,我得到:

Content controls have to be top-level controls in a content page or a nested master page that references a master page.
HttpException (0x80004005): Content controls have to be top-level controls in a content page or a nested master page that references a master page.]
   System.Web.UI.MasterPage.CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection) +8665016
   System.Web.UI.Page.get_Master() +51
   System.Web.UI.Page.ApplyMasterPage() +15
   System.Web.UI.Page.PerformPreInit() +45
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +282

如果我在该解决方案之外的独立项目中执行相同的操作,则它可以正常工作。 请记住,我使用的是 Web 应用程序项目与网站项目,如果这有什么区别的话。

网络表单:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebUI._Default" MasterPageFile="~/Site1.Master" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

母版页:

<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Site1.master.vb" Inherits="WebUI.Site1" %>

<!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></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

This is weird. I added a brand new Web Application project to my solution in Visual Studio 2008.

Created a master page. Made zero modifications.
Created a new webform. Set its master page to the MP I just created.

Still, no modifications. No markup. No user controls. No references. Nothing. However when I try to run it, I get:

Content controls have to be top-level controls in a content page or a nested master page that references a master page.
HttpException (0x80004005): Content controls have to be top-level controls in a content page or a nested master page that references a master page.]
   System.Web.UI.MasterPage.CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection) +8665016
   System.Web.UI.Page.get_Master() +51
   System.Web.UI.Page.ApplyMasterPage() +15
   System.Web.UI.Page.PerformPreInit() +45
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +282

If I do the same exact thing in a standalone project thats outside of this solution, it works fine. Keep in mind that I'm using a web application project vs a website project if that makes any difference.

The webform:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebUI._Default" MasterPageFile="~/Site1.Master" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

The master page:

<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Site1.master.vb" Inherits="WebUI.Site1" %>

<!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></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

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

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

发布评论

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

评论(13

一杆小烟枪 2024-07-26 07:16:12

您的网络表单不应包含所有这些标记(例如 标记)。 由于它有一个母版页,因此您只需从内容标签开始。 您的 aspx 页面应如下所示:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebUI._Default" MasterPageFile="~/Site1.Master" %>

<asp:content id="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
   This is the body!
</asp:content>

添加新的 aspx 页面时,请确保选中“添加新项目”对话框中的“选择母版页”。

Your web form shouldn't have all of that markup (like the <html> tag). Since it has a master page, you just start with the content tag. Your aspx page should look like this:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebUI._Default" MasterPageFile="~/Site1.Master" %>

<asp:content id="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
   This is the body!
</asp:content>

When you're adding a new aspx page make sure to check "select master page" in the "add new item" dialog.

云朵有点甜 2024-07-26 07:16:12

这是使用 Visual Studio 的另一种方法:如果您在 Visual Studio 中执行“新建项目”并选择 Web 表单,它将创建一个独立的 *.aspx Web 表单,这就是您当前的 Web 表单(这是您所做的吗?) 。 您需要选择“Web 内容表单”,然后选择要附加到其上的母版页。

Here's another way using Visual Studio: If you do New Item in Visual Studio and you select Web Form, it will create a standalone *.aspx web form, which is what you have for your current web form (is this what you did?). You need to select Web Content Form and then select the master page you want attached to it.

晨敛清荷 2024-07-26 07:16:12

您的 Web 表单应如下所示:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebUI._Default" MasterPageFile="~/Site1.Master" %>
<asp:Content runat="server" ID="head" ContentPlaceHolderId="head">
<!-- stuff you want in >head%lt; -->
</asp:Content>

<asp:Content runat="server" ID="content" ContentPlaceHolderId="ContentPlaceHolder1">
<h1>Your content</h1>
</asp:Content>

请注意,没有 标记

Your web form should look like this:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebUI._Default" MasterPageFile="~/Site1.Master" %>
<asp:Content runat="server" ID="head" ContentPlaceHolderId="head">
<!-- stuff you want in >head%lt; -->
</asp:Content>

<asp:Content runat="server" ID="content" ContentPlaceHolderId="ContentPlaceHolder1">
<h1>Your content</h1>
</asp:Content>

Note that there is no <html> tag

你是我的挚爱i 2024-07-26 07:16:12

创建 WebForm 时,您是否在“添加新项目”对话框本身中选择了它所附加的母版页? 或者您是否使用 @Page 指令的 MasterPageFile 属性手动附加它? 如果是后者,它可能会解释您收到的错误消息。

VS 会自动在每种页面中插入一定的标记。 如果您在创建页面时选择 MasterPage,则除了 @Page 声明和顶级内容控件之外,它不会生成任何标记。

When you created the WebForm, did you select the Master page it is attached to in the "Add New Item" dialog itself ? Or did you attach it manually using the MasterPageFile attribute of the @Page directive ? If it was the latter, it might explain the error message you receive.

VS automatically inserts certain markup in each kind of page. If you select the MasterPage at the time of page creation itself, it does not generate any markup except the @Page declaration and the top level Content control.

笙痞 2024-07-26 07:16:12

由于某种原因,没有选项
创建页面对话框来选择
母版页。 我都尝试过
以编程方式声明 MP 和
通过更新属性
属性窗格。 – 无承运人 13 分钟

我相信这是因为我正在使用“网络
应用程序”与“网站”–
NoCarrier 9 分钟前

很可能它位于 <@PAGE> 中。 标记您的问题所在。 也就是说,无论您是否使用 Web 应用程序,都没有什么区别。 要创建子页面,请右键单击解决方案资源管理器中的母版页,然后选择添加内容页面。

For some reason, there is no option in
the create page dialogue to select a
master page. I have tried both
programatically declaring the MP and
by updating the property in the
Properties pane. – NoCarrier 13 mins
ago

I believe its because i'm using a "web
application" vs a "web site" –
NoCarrier 9 mins ago

Chances are it is in the <@PAGE> tag where your problem is. That said, it doesnt make a difference if you are using a Web Application or not. To create a Child Page, right click on your master page in the Solution Explorer and choose Add Content Page.

2024-07-26 07:16:12

刚刚遇到这个问题。
这是因为我们有一个以双斜杠结尾的标签:

<//asp:HyperLink>

Just got this problem.
It was because we had a tag ending with double slashes:

<//asp:HyperLink>
吾家有女初长成 2024-07-26 07:16:12

我刚刚遇到了这个异常,在我的例子中,它是由 asp:content 元素之间的空格引起的,

所以,这失败了:

<asp:content runat="server" ContentPlaceHolderID="Header">
    Header
</asp:content>

<asp:Content runat="server" ContentPlaceHolderID="Content">
    Content
</asp:Content>

但是删除元素之间的空格有效:

<asp:content runat="server" ContentPlaceHolderID="Header">
    Header
</asp:content><asp:Content runat="server" ContentPlaceHolderID="Content">
    Content
</asp:Content>

I just encountered this exception and in my case it was cause by a white space between asp:content elements

So, this failed:

<asp:content runat="server" ContentPlaceHolderID="Header">
    Header
</asp:content>

<asp:Content runat="server" ContentPlaceHolderID="Content">
    Content
</asp:Content>

But removing the white spaces between the elements worked:

<asp:content runat="server" ContentPlaceHolderID="Header">
    Header
</asp:content><asp:Content runat="server" ContentPlaceHolderID="Content">
    Content
</asp:Content>
夜声 2024-07-26 07:16:12

我想迟到总比不到好...您只有在开发网站时才可以选择设置主页 (FILE>NEW>WEBSITE)...但当您创建 ASP.NET 项目时(FILE>NEW>WEBSITE)则不能设置 MASTERPAGE。 PROJECT) - 您必须使用新创建的 Webform 的属性来设置母版页,并且由您来修改 ASPX 源以使其符合母版页(即删除库存 HTML 等...)

Better late than never i suppose... you get the option to set the MASTERPAGE only of you are developing a WEB SITE (FILE>NEW>WEBSITE)... but not when you create an ASP.NET project (FILE>NEW>PROJECT) - there you have to set the Masterpage using the properties of the newly created webform and it's up to you to modify the ASPX source to make it master page compliant (ie, removing the stock HTML, etc...)

浮云落日 2024-07-26 07:16:12
protected void Page_PreInit(object sender, EventArgs e) 
{ 
 if (Membership.GetUser() == null) //check the user weather user is logged in or not
    this.Page.MasterPageFile = "~/General.master";
 else
    this.Page.MasterPageFile = "~/myMaster.master";
}
protected void Page_PreInit(object sender, EventArgs e) 
{ 
 if (Membership.GetUser() == null) //check the user weather user is logged in or not
    this.Page.MasterPageFile = "~/General.master";
 else
    this.Page.MasterPageFile = "~/myMaster.master";
}
月下客 2024-07-26 07:16:12

我在 SharePoint Designer 2013 中编辑 Web 部件 (.aspx) 页面后遇到此错误。当我查看 SPD 中的代码时,页面顶部附近的 H1 元素突出显示为黄色。 将鼠标悬停在其上表明 SharePoint:AjaxDelta 在 H1 之前尚未关闭。 添加 修复了该问题。

很奇怪,因为在我在页面其他位置处理列表视图 Web 部件或页面查看器 Web 部件后,SPD 似乎引入了错误。

I encountered this error after editing a web part (.aspx) page in SharePoint Designer 2013. When I looked at the code in SPD, an H1 element near the top of the page was highlighted yellow. Hovering over that indicated that SharePoint:AjaxDelta was not closed before the H1. Adding the </SharePoint:AjaxDelta> fixed it.

Weird because it appeared SPD introduced the error after I was working on listview web parts or a page viewer web part elsewhere on the page.

我三岁 2024-07-26 07:16:12

您需要添加asp内容并添加与母版页中的占位符相对应的内容占位符id

您可以阅读此链接了解更多详细信息

You need to add asp content and add content place holder id correspond to the placeholder in master page.

You can read this link for more detail

〗斷ホ乔殘χμё〖 2024-07-26 07:16:12

就我而言,我在页面中动态加载用户控件,并且页面和用户控件都有内容标签,

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

从用户控件中删除此标签对我有用。

In my case I was loading a user control dynamically in a page and both the page and user control had the content tags

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

Removing this tag from the user control worked for me.

蘑菇王子 2024-07-26 07:16:12

就我而言,asp:content 标记中缺少 runat="server"。 我知道这很愚蠢,但有人可能已经从我得到的构建中的代码中删除了。

这对我有用。 有人可能面临同样的事情。 因此共享。

In my case runat="server" was missing in the asp:content tag. I know it's stupid, but someone might have removed from the code in the build I got.

It worked for me. Someone may face the same thing. Hence shared.

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