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

67

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>
asp.net
visual-studio-2008
master-pages
asked on Stack Overflow May 7, 2009 by NoCarrier • edited Jul 21, 2016 by meJustAndrew

13 Answers

81

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.

answered on Stack Overflow May 7, 2009 by JerSchneid
57

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.

answered on Stack Overflow May 7, 2009 by gabe • edited Feb 6, 2014 by George Stocker
7

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>

Note that there is no <html> tag

answered on Stack Overflow May 7, 2009 by Joshua Shannon • edited Jul 21, 2016 by meJustAndrew
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.

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.

answered on Stack Overflow May 7, 2009 by Cerebrus
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

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.

answered on Stack Overflow May 7, 2009 by Serapth
3

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

<//asp:HyperLink>
answered on Stack Overflow Nov 29, 2010 by Daniel Ryan
3

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>
answered on Stack Overflow Jan 31, 2013 by Thorgeir • edited Jan 31, 2013 by Thorgeir
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...)

answered on Stack Overflow Aug 11, 2011 by Bill Roberts
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";
}
answered on Stack Overflow Dec 14, 2012 by Mike • edited Dec 14, 2012 by Mxyk
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.

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.

answered on Stack Overflow Feb 5, 2016 by G. Laushine
1

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

answered on Stack Overflow May 9, 2019 by Pajri Aprilio
0

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.

answered on Stack Overflow May 2, 2019 by Mohammed Siddiq
0

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.

answered on Stack Overflow Nov 23, 2020 by Sudeep nayak

User contributions licensed under CC BY-SA 3.0