Masterpage Weirdness - "Content controls must be top-level elements on the content page or a sub-master page that links to the master page."

This is strange. I added a new web application project for my solution in Visual Studio 2008.

Created the main page. Made zero changes. Created a new web form. Set your homepage in the MP I just created.

However, no change. No markup. No user controls. No links. 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 in a standalone project that is outside of this solution, it works great. Keep in mind that I am using a web application project and a website project, if that matters.

Web Form:

 <%@ 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> 

Main 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> 
+63
visual-studio-2008 master-pages
May 07 '09 at 17:02
source share
12 answers

Another way to use Visual Studio: if you create a new element in Visual Studio and you select Web Form, it will create a standalone * .aspx web form that you have for your current web form (is that what you did?). You need to select the web content form, and then select the main page to which you want to connect.

+53
May 7 '09 at 17:32
source share

Your web form should not contain all this markup (for example, the <html> ). Since it has a homepage, you start with a 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 add a new aspx page, be sure to check the "Select Home Page" in the "Add New Item" dialog box.

+76
May 07 '09 at 17:24
source share

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 &gt;head%lt; --> </asp:Content> <asp:Content runat="server" ID="content" ContentPlaceHolderId="ContentPlaceHolder1"> <h1>Your content</h1> </asp:Content> 

Please note that there is no <html>

+6
May 07 '09 at 17:25
source share

When you created WebForm, did you select the main page to which it is attached in the Add New Item dialog box? Or did you manually attach it using the MasterPageFile attribute of the @Page directive? If this is the last, this may explain the error message you receive.

VS automatically inserts specific markup into each page view. If you select MasterPage during page creation, it does not generate markup except for the @Page and the top-level Content control.

+4
May 07 '09 at 17:11
source share

For some reason, in the page creation dialog to select the main page. I tried both programmatically declaring the MP and by updating the property in the Property Panel. - NoCarrier 13 minutes back

I believe because I use the website app "against the" website "- NoCarrier 9 minutes ago

Most likely, this is in the <@PAGE> tag where your problem is located. However, it does not matter if you are using a web application or not. To create a child page, right-click on the main page in Solution Explorer and select "Add Content Page".

+4
May 07 '09 at 17:30
source share

Just this problem. This was because we had a tag ending in double slashes:

 <//asp:HyperLink> 
+3
Nov 29 '10 at 4:03
source share

I just came across this exception, and in my case it caused a 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 spaces between processed elements:

 <asp:content runat="server" ContentPlaceHolderID="Header"> Header </asp:content><asp:Content runat="server" ContentPlaceHolderID="Content"> Content </asp:Content> 
+3
Jan 31 '13 at 15:46
source share

Better late than never, I suppose ... you get the opportunity to install only MASTERPAGE that you are developing a WEB SITE (FILE> NEW> WEBSITE) ... but not when creating an ASP.NET project (FILE> NEW> PROJECT) - you need to set up the master page using the properties of the newly created web form and it is up to you to change the ASPX source to make it compatible with the main page (i.e. deleting the HTML file, etc.).

+2
Aug 11 '11 at 15:50
source share
 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"; } 
+2
Dec 14 '12 at 17:50
source share

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

Strange, because the SPD error message came up after I worked on listview websites or the webview web part elsewhere on the page.

+1
Feb 05 '16 at 17:21
source share

In my case, I loaded the user control dynamically on the page, and both the page and the user control had content tags

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

Removing this tag from a user control worked for me.

0
May 2, '19 at 21:48
source share

You need to add asp content and add the content placeholder identifier corresponding to the placeholder on the main page.

You can read this link for more details.

0
May 09 '19 at 4:45
source share



All Articles