主页怪异——“内容控件必须是内容页面或引用母版页面的嵌套母版页面中的*控件。”

时间:2022-01-23 15:55:08

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.

创建了一个主页。做任何修改。创建了一个新的webform。将它的母版页设置为我刚才创建的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应用程序项目,而不是web项目。

The webform:

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>

10 个解决方案

#1


50  

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.

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

#2


73  

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:

web表单不应该包含所有这些标记(比如标记)。因为它有一个主页面,所以您只需从content标记开始。您的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页面时,请确保在“添加新项”对话框中检查“选择母版页”。

#3


6  

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

注意,没有标记

#4


4  

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.

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

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.

VS自动地在每种页面中插入某些标记。如果在创建页面时选择MasterPage,那么除了@Page声明和顶层内容控制之外,它不会生成任何标记。

#5


4  

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

出于某种原因,在create page对话框中没有选择选择母版页的选项。我尝试通过编程声明MP和更新Properties窗格中的属性。- 13分钟前还没有

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

我相信这是因为我使用的是“web应用程序”而不是“web站点”——9分钟前还没有

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.

很可能它位于问题所在的<@PAGE>标记中。也就是说,如果你使用的是Web应用程序,这并没有什么区别。要创建子页面,请在解决方案资源管理器中右键单击母版页面并选择Add Content Page。

#6


3  

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

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

<//asp:HyperLink>

#7


3  

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>

#8


2  

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

迟做总比不做好……您可以选择设置主页,只有您正在开发一个网站(文件>新的>网站)……但是当你创建一个ASP时就不是这样了。NET项目(文件>新>项目)-你必须使用新创建的webform的属性来设置Masterpage,并且你需要修改ASPX源代码以使它能够兼容页面(例如,删除stock HTML,等等)。

#9


2  

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";
}

#10


1  

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.

我在SharePoint Designer 2013中编辑了一个web part (.aspx)页面后遇到了这个错误。当我查看SPD的代码时,页面顶部的H1元素被突出显示为黄色。盘旋在上面表明SharePoint:AjaxDelta在H1之前没有关闭。添加修复了它。

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.

奇怪的是,在我处理listview web部件或页面查看器web部件之后,SPD似乎引入了这个错误。

#1


50  

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.

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

#2


73  

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:

web表单不应该包含所有这些标记(比如标记)。因为它有一个主页面,所以您只需从content标记开始。您的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页面时,请确保在“添加新项”对话框中检查“选择母版页”。

#3


6  

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

注意,没有标记

#4


4  

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.

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

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.

VS自动地在每种页面中插入某些标记。如果在创建页面时选择MasterPage,那么除了@Page声明和顶层内容控制之外,它不会生成任何标记。

#5


4  

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

出于某种原因,在create page对话框中没有选择选择母版页的选项。我尝试通过编程声明MP和更新Properties窗格中的属性。- 13分钟前还没有

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

我相信这是因为我使用的是“web应用程序”而不是“web站点”——9分钟前还没有

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.

很可能它位于问题所在的<@PAGE>标记中。也就是说,如果你使用的是Web应用程序,这并没有什么区别。要创建子页面,请在解决方案资源管理器中右键单击母版页面并选择Add Content Page。

#6


3  

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

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

<//asp:HyperLink>

#7


3  

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>

#8


2  

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

迟做总比不做好……您可以选择设置主页,只有您正在开发一个网站(文件>新的>网站)……但是当你创建一个ASP时就不是这样了。NET项目(文件>新>项目)-你必须使用新创建的webform的属性来设置Masterpage,并且你需要修改ASPX源代码以使它能够兼容页面(例如,删除stock HTML,等等)。

#9


2  

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";
}

#10


1  

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.

我在SharePoint Designer 2013中编辑了一个web part (.aspx)页面后遇到了这个错误。当我查看SPD的代码时,页面顶部的H1元素被突出显示为黄色。盘旋在上面表明SharePoint:AjaxDelta在H1之前没有关闭。添加修复了它。

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.

奇怪的是,在我处理listview web部件或页面查看器web部件之后,SPD似乎引入了这个错误。