R – Why do the ASP.NET Master pages dont compile in content pages

asp.netmaster-pages

I'm writing my first ASP.NET web application containing Master Pages. However, even though I seem to use them by the book, when running the project the Master Pages don't seem to work.

I have created a Default.MasterPage lite this:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Default.master.cs" Inherits="TimeTracker.Default" %>

<!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>My new pager</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
            <tr>
                <asp:ContentPlaceHolder ID="PageHeader" runat="server">   
                </asp:ContentPlaceHolder>
            </tr>
            <tr>
            <asp:ContentPlaceHolder ID="Navigation" runat="server">
            </asp:ContentPlaceHolder>
            </tr>
            <tr>
                <asp:ContentPlaceHolder ID="Main" runat="server">  
                </asp:ContentPlaceHolder>
            </tr>
            <tr>
                <asp:ContentPlaceHolder ID="Footer" runat="server">  
                </asp:ContentPlaceHolder>
            </tr>      
            </table>     
        </div>
    </form>
</body>
</html>

And two content pages like this:
(PageHeader.aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageHeader.aspx.cs" Inherits="TimeTracker.PageHeader"  MasterPageFile="~/Default.Master" Title="Header"%>

<asp:Content ID="Header" ContentPlaceHolderID="PageHeader" runat="server">
        Enalog Time-Tracker
</asp:Content>

And (Default.aspx)

<%@ Page Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TimeTracker._Default" Title="Login"%>

<asp:Content ID="LoginPage" ContentPlaceHolderID="Main" runat="server">
Login page
</asp:Content>

But when running the project, the MasterPage don't compile in the content pages, even though I believe I've wired them up correctly. So if I for example runs Default.aspx, I only see is the content from the ContentPlaceHolderID="Main", and if I run PageHeader.aspx, I only see the content from the ContentPlaceHolderID="PageHeader".
Do anyone know why I get this behaviour, or perhaps what I'm doing wrong here?
Thanks in advance!

Best Answer

You need to put both sections in your content pages, like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageHeader.aspx.cs"
    Inherits="TimeTracker._Default"  MasterPageFile="~/Default.Master" 
    Title="Login"%>

<asp:Content ID="HeaderContent" ContentPlaceHolderID="PageHeader" runat="server">
   This is my header
</asp:Content>

<asp:Content ID="MainContent" ContentPlaceHolderID="Main" runat="server">
   My main page content is here.
</asp:Content>

You probably don't need the "Header" page.

Related Topic