Content Page Code
Content Page (MasterPageTest.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MasterPageTest.aspx.cs" Inherits="MasterPageTest" MasterPageFile="~/MasterPageTest.master" %>
<asp:content ContentPlaceHolderId="cphPageTitle" runat="server">
Here's my Title!
</asp:content>
<asp:content ContentPlaceHolderId="cphPageHeading" runat="server">
Here's my 2<sup>nd</sup> Title!
</asp:content>
<asp:content ContentPlaceHolderId="cphDoesntExist" runat="server">
Look all you want, but you won't find this anywhere on the webpage. A content tag must have a ContentPlaceHolder tag to go into. The server sees the master page when the content page pulls it in, and because the master page (see above code for the .master file) does not have an <asp:contentplaceholder id="cphDoesntExist" runat="server">, this content block causes an application error. If you are reading this, then this content tag has been removed and doesn't exist! :) Weirdly cool.
</asp:content>
<asp:content ContentPlaceHolderId="cphMainContent" runat="server">
...content...
</asp:content>
<asp:content ContentPlaceHolderId="cphEmptyContentTag" runat="server" />
Note how amazingly simple the content page is. It's a page directive, and a series of <asp:content> tags. Everything else is in the master page. The ContentPlaceHolderId attribute of the <asp:content> tag ties the the tag (and it's associated content) back to the master page's ContentPlaceHolder control whose id matches. It needs a lot of work, but this is a test. It certainly shows the potential of ASP.NET master pages.
OK, some more details. There are a couple of things that didn't even pan out well enough to show how they break. One, using the same Id for two ContentPlaceHolder controls in the master page seemed like a great way to insert the same piece of text into two places in the document. I was trying it with the main <h1> and the the <title> in the header. But the application error .NET threw when it tried to run with duplicate Ids was less desirable, so I threw in the towel with that one.
Unfairly, you will note that we were able to place content into a ContentPlaceHolder tag on the master page and display it even though there is no corresponding content tag in the content page. In fact, if there were, the content page would override what's available in the master page. However, If we add a content tag in the content page that has no corresponding ContentPlaceHolder tag on the master page, it not only doesn't display but creates an application error.
Also, as you would expect, a ContentPlaceHolder tag refused to fit into the content attribute of my meta keywords tag. The angle brackets (<>) and quotation marks of the ContentPlaceHolder wreak havoc with attribute value that we're trying to insert. It would have been handy to just dump it straight in, but that's not within the power of master pages. Looks like I'd have to use a database system to pull those keywords in a future update.
Skip to Main Points