简体   繁体   English

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

[英]Master Page Weirdness - “Content controls have to be top-level controls in a content page or a nested master page that references a master page.”

This is weird.这很奇怪。 I added a brand new Web Application project to my solution in Visual Studio 2008.我在 Visual Studio 2008 的解决方案中添加了一个全新的 Web 应用程序项目。

Created a master page.创建了一个母版页。 Made zero modifications.进行零修改。 Created a new webform.创建了一个新的网络表单。 Set its master page to the MP I just created.将其母版页设置为我刚刚创建的 MP。

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.请记住,如果有任何区别,我使用的是 Web 应用程序项目与网站项目。

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>

Your web form shouldn't have all of that markup (like the <html> tag).您的 Web 表单不应包含所有这些标记(如<html>标记)。 Since it has a master page, you just start with the content tag.由于它有一个母版页,您只需从内容标签开始。 Your aspx page should look like this:您的 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>

When you're adding a new aspx page make sure to check "select master page" in the "add new item" dialog.添加新的 aspx 页面时,请确保选中“添加新项目”对话框中的“选择母版页”。

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?).这是使用 Visual Studio 的另一种方法:如果您在 Visual Studio 中执行 New Item 并选择 Web Form,它将创建一个独立的 *.aspx Web 表单,这是您当前 Web 表单所拥有的(这是您所做的吗?) . You need to select Web Content Form and then select the master page you want attached to it.您需要选择 Web 内容表单,然后选择要附加到它的母版页。

Your web form should look like this:您的 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 &gt;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注意没有<html>标签

When you created the WebForm, did you select the Master page it is attached to in the "Add New Item" dialog itself ?当您创建 WebForm 时,您是否在“添加新项目”对话框中选择了它所附加的母版页? Or did you attach it manually using the MasterPageFile attribute of the @Page directive ?或者您是否使用@Page指令的MasterPageFile属性手动附加它? If it was the latter, it might explain the error message you receive.如果是后者,它可能会解释您收到的错误消息。

VS automatically inserts certain markup in each kind of page. VS 会自动在每种页面中插入某些标记。 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.如果您在创建页面时选择 MasterPage,它不会生成除@Page声明和顶级内容控件之外的任何标记。

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.我已经尝试以编程方式声明 MP 和更新属性窗格中的属性。 – NoCarrier 13 mins ago – NoCarrier 13 分钟前

I believe its because i'm using a "web application" vs a "web site" – NoCarrier 9 mins ago我相信这是因为我使用的是“网络应用程序”与“网站”——NoCarrier 9 分钟前

Chances are it is in the <@PAGE> tag where your problem is.很可能它在您的问题所在的 <@PAGE> 标签中。 That said, it doesnt make a difference if you are using a Web Application or not.也就是说,是否使用 Web 应用程序并没有什么区别。 To create a Child Page, right click on your master page in the Solution Explorer and choose Add Content Page.要创建子页面,请在解决方案资源管理器中右键单击您的母版页面,然后选择添加内容页面。

I just encountered this exception and in my case it was cause by a white space between asp:content elements我刚刚遇到了这个异常,就我而言,它是由 asp:content 元素之间的空白引起的

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>

Just got this problem.刚遇到这个问题。 It was because we had a tag ending with double slashes:这是因为我们有一个以双斜线结尾的标签:

<//asp:HyperLink>
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";
}

我想迟到总比没有好......你可以选择设置MASTERPAGE,只有你正在开发一个网站(文件>新>网站)......但不是当你创建一个ASP.NET项目(文件>新>项目)-您必须使用新创建的网络表单的属性设置母版页,并且由您修改 ASPX 源以使其与母版页兼容(即删除库存 HTML 等...)

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.我在 SharePoint Designer 2013 中编辑 Web 部件 (.aspx) 页面后遇到此错误。当我查看 SPD 中的代码时,页面顶部附近的 H1 元素以黄色突出显示。 Hovering over that indicated that SharePoint:AjaxDelta was not closed before the H1.将鼠标悬停在其上表示 SharePoint:AjaxDelta 在 H1 之前未关闭。 Adding the </SharePoint:AjaxDelta> fixed it.添加</SharePoint:AjaxDelta>修复了它。

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.很奇怪,因为在我处理列表视图 Web 部件或页面其他位置的页面查看器 Web 部件后,SPD 似乎引入了该错误。

You need to add asp content and add content place holder id correspond to the placeholder in master page.您需要添加asp内容并添加与母版页中的占位符对应的内容占位符id

You can read this link for more detail您可以阅读此链接了解更多详情

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.从用户控件中删除此标签对我有用。

In my case runat="server" was missing in the asp:content tag.在我的例子中, asp:content 标签中缺少runat="server" 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.因此共享。

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

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