[asp.net] 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.

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>

This question is related to asp.net visual-studio-2008 master-pages

The answer is


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.


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


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.


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.


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.


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


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

<//asp:HyperLink>

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.


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

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>

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.


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


Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to visual-studio-2008

Convert Text to Uppercase while typing in Text box SQL Server r2 installation error .. update Visual Studio 2008 to SP1 What is and how to fix System.TypeInitializationException error? Error LNK2019: Unresolved External Symbol in Visual Studio download and install visual studio 2008 Git in Visual Studio - add existing project? Visual Studio can't 'see' my included header files How to insert Records in Database using C# language? If statements for Checkboxes LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

Examples related to master-pages

how to access master page control from content page Make absolute positioned div expand parent div height MVC 3: How to render a view without its layout page when loaded via ajax? a page can have only one server-side form tag Parser Error: '_Default' is not allowed here because it does not extend class 'System.Web.UI.Page' & MasterType declaration how to display none through code behind Where does error CS0433 "Type 'X' already exists in both A.dll and B.dll " come from? How to include Javascript file in Asp.Net page 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." Can I dynamically add HTML within a div tag from C# on load event?