I learned something new today, thought I’d share it. Actually, I think I relearned it, it sounded familiar. There are so many features to .Net these days it is tough to remember everything. Anyway, here we go.
Did you know that you can put content directly inside a ContentPlaceHolder of a MasterPage? I’m not talking about in a page’s asp:Content control that matches up to the Master’s asp:ContentPlaceHolder. That is what we typically do. But you can put content in the asp:ContentPlaceHolder control on the master. I like to think of it as default content. In the content page, if you put content inside the asp:Content control, it will override the "default" value from the Master. If not, the Master’s content will be shown. Content, Content Pages, asp:Content, asp:ContentPlaceholders…confused? Here’s a simple example:
Here is some code from body of our MasterPage. I’m only including the body to save space. We have 2 contentplaceholders. The first has some "default text"
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
By Default, show this.
</asp:ContentPlaceHolder>
</div>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
Now let’s look at our first ContentPage. It puts text in each of the Content areas:
<%@ Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs" Inherits="MasterPageSample.WebForm1"
Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
content in placeholder 1
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
content in placeholder 2
</asp:Content>
Next comes our second ContentPage. This one only has one asp:Content control to match the second ContentPlaceHolder from the MasterPage:
<%@ Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
CodeBehind="WebForm2.aspx.cs" Inherits="MasterPageSample.WebForm2"
Title="Untitled Page" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
content in placeholder 2
</asp:Content>
The last ContentPage has 2 asp:Content controls but only the second one has actual content. The first one is empty:
<%@ Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
CodeBehind="WebForm3.aspx.cs" Inherits="MasterPageSample.WebForm3"
Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
content in placeholder 2
</asp:Content>
So what are the results? The first page renders both pieces of content:
The second one shows it’s content and since the first ContentPlaceholder wasn’t matched up with a Content control, it shows the content from the MasterPage:
The last page has both ContentPlaceHolders matched up, only the first has no content at all. So in this case, it overrides the "default" content from the master with nothing! All you get is the content from the second Content control:
Pretty cool, huh?