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>
The master page establishes a layout and includes one or more ContentPlaceHolder controls for replaceable text and controls. The content page includes only the text and controls that are merged at run time with the master page's ContentPlaceHolder controls.
To access the master page controls we need to use the Master key word then call the Property method of control. As we use Master. EnterNameTextBox() to access the master page EnterName textbox and Master. SendDataToContentPageButton() method is used to acees master page button.
Master pages at run time From a programming perspective, the two pages act as separate containers for their respective controls. The content page acts as a container for the master page. However, you can reference public master-page members from code in the content page, as described in the next section.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With