母版页怪异-“内容控件必须是内容页或引用母版页的嵌套母版页中的顶级控件。”
这很奇怪。 我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您的网络表单不应包含所有这些标记(例如
标记)。 由于它有一个母版页,因此您只需从内容标签开始。 您的 aspx 页面应如下所示:
添加新的 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:When you're adding a new aspx page make sure to check "select master page" in the "add new item" dialog.
这是使用 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.
您的 Web 表单应如下所示:
请注意,没有
标记
Your web form should look like this:
Note that there is no
<html>
tag创建 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.很可能它位于 <@PAGE> 中。 标记您的问题所在。 也就是说,无论您是否使用 Web 应用程序,都没有什么区别。 要创建子页面,请右键单击解决方案资源管理器中的母版页,然后选择添加内容页面。
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.
刚刚遇到这个问题。
这是因为我们有一个以双斜杠结尾的标签:
Just got this problem.
It was because we had a tag ending with double slashes:
我刚刚遇到了这个异常,在我的例子中,它是由 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:
But removing the white spaces between the elements worked:
我想迟到总比不到好...您只有在开发网站时才可以选择设置主页 (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...)
我在 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.
您需要添加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
就我而言,我在页面中动态加载用户控件,并且页面和用户控件都有内容标签,
从用户控件中删除此标签对我有用。
In my case I was loading a user control dynamically in a page and both the page and user control had the content tags
Removing this tag from the user control worked for me.
就我而言,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.