Hey Guys,
I've made a couple of web pages and now i want to add a master page to all the pages. But when i tried to add the master page the following error occured
"Content controls have to be top-level controls in a content page or a nested master page that references a master page."

FYI i did not check the check box "Select Master Page" when i was making the pages. Does this make a difference??
Please suggest how i could add master pages to my web pages??

Dani AI

Generated

Short diagnosis and targeted checks — the error means the ASP.NET parser found content controls that are not the only top-level server elements in the content page. and pointed out the usual fixes; the following clarifications and checks address the common causes that still trigger this error after the basic steps.

Open the .aspx in Source view and verify the file is a true content page (only the @Page directive followed by top-level asp:Content elements). Typical problems to remove or relocate into asp:Content blocks:

  • The full page skeleton (DOCTYPE, <html>, <head>, <body>) left in the child page — that markup belongs in the master.
  • Any <form runat="server"> left in the child when the master already supplies the form.
  • Server controls or literal text that appear before the first asp:Content or between content blocks.
  • Incorrect ContentPlaceHolder IDs (they must exactly match the IDs in the master page).

Visual Studio quirks after conversion: converting an existing page often breaks the designer declarations. A clean/rebuild can help; for Web Application projects, right‑click the page and use “Convert to Web Application” to regenerate the .designer file. Saving the .aspx in Design view sometimes forces Visual Studio to refresh control declarations.

Master-side reminder: a master page exposes placeholders like this (example):

<asp:ContentPlaceHolder ID="MainContent" runat="server">
    <!-- optional default content -->
</asp:ContentPlaceHolder>

Everything intended for that area must be inside the matching content block on the child page.

Summary: ’s advice is correct; when the error persists, focus on removing any top-level markup or server controls outside asp:Content, confirm placeholder ID matches, and regenerate the designer/ rebuild the project so the page and code-behind remain in sync.

Recommended Answers

All 4 Replies

If you want to add Master Page to your existing web pages, the do the following things:

1. set the MasterPageFile property to your .master file name.
2. Remove the form tage from the child page if the master page already has the form tag.
3. Wrap controls in the child page with a Content control

<asp:Content id="myContent" runat="server" ContentPlaceholderId="MasterID">  page contents</asp:Content>

The value of ContentPlaceHolderID should be the ID of the ContentPlaceHolder control from the master page.

Also check this link: http://asptutorials.net/ASP/masterpages/

Thanks dude..that really helped

If your question is answered, please mark this thread as solved.

try giving out the content page same as that given to the master page id. also see the content page has the master page file directive

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.