|
Back
Error: Content controls have to be top-level controls in a content page or a nested master page that references a master page
Problem
This problem is related to the use of master pages in ASP.net 2/Visual Studio 2005.
The following error message occurs when you view the page in your web browser:
- Content controls have to be top-level controls in a content page or a nested master page that references a master page
Why the error occurs
The error will occur if you create an aspx page in Visual Studio 2005 and do not associate the page with a master page from the outset.
ie You create a page, say newpage.aspx. You then want to indicate that newpage.aspx should be associating itself with a master page you have already created. So in the bottom-right hand corner of your Visual Studio interface, in the Properties section, you change the MasterPageFile property to the master page you want.
Resolution
There are two methods of resolving this problem:
Method 1
- Delete the page and create a new one. When you create a new aspx page, you should get a check box in the Add New Item dialogue box called Select Master Page. Click on this and then click on Add. You can then select the master page you want to use for the aspx page.
Method 2
The second method is to amend the existing aspx you have created. You might want to use this method if you have already done alot of work on the page.
You need to remove the <html>, <head>, <body>
and <form> tags - these can be removed because they will already exist in your master page.
Just leave the top line of document and the parts that are specific to your page functionality. ie in the example below all the items you need to remove are highlighted in red.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" MasterPageFile="~/TestMaster.master" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
Main Body of the form
<asp:TextBox runat="server">
</asp:TextBox>
<asp:Button runat="server" Text="Button" />
</div>
</form>
</body>
</html>
The content of your page then needs to be placed within the <asp:Content> tag. Hence your final page will look like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" MasterPageFile="~/TestMaster.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Main Body of the form
<asp:TextBox runat="server"> </asp:TextBox> <asp:Button runat="server" Text="Button" /> </div> </asp:Content>
Note that the ContentPlaceHolderId value of your <asp:Content> tag in you aspx page must be the same as that of the Id value of the <asp:contentplaceholder> tag in your master page.
Thus if in your master page the <asp:contentplaceholder> tag is as follows:
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> </asp:contentplaceholder>
In your aspx page it must be:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
Further Information For further information on master pages see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/masterpages.asp.
Back
|