0

I am trying to create a Main.master page with two ContentPlaceHolder sections. When I load the default page, it only renders ContentPlaceHolder1, I have to actually load Second.aspx to see the second ContentPlaceHolder. Why?

In my Main.master I have:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Main.master.cs" Inherits="Main" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
        <asp:ContentPlaceHolder id="ContentPlaceHolder2" runat="server"></asp:ContentPlaceHolder>
    </div>
</body>
</html>

Additionally, I have created two additional pages Default.aspx and Second.aspx:

Detault:

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
        HOW HOW HOW HOW
</asp:Content>

Other page is

 Second:
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="Second.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    COW COW COW COW
</asp:Content>

Its only rendering the first PlaceHolder, how can I have separate content files and have the both rendered on the same page?

enter image description here

Bob R
  • 605
  • 1
  • 13
  • 25

1 Answers1

0

You have to ask yourself: If you navigate to Default.aspx, how will your application know to grab the content in Second.aspx? Simply put: it won't.

First, here's the MSDN on the ContentPlaceHolder.

You can have as many ContentPlaceHolders on your masterpage as you want, and EACH page can either render into these content areas or NOT.

So your Default.aspx can look like:

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
        HOW HOW HOW HOW
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    COW COW COW COW
</asp:Content>

And you will get the results you are looking for.

Now, there are ways you can render an OUTSIDE html file INTO one of these content placeholders (one way would be javascript) - but (and please, someone correct me if I'm wrong) there won't be a way to do it with an ASPX webpage, codebehind or not.

To touch on your question:

how can I have separate content files and have the both rendered on the same page?

You may want to look into this: How to include a partial view inside a webform

Community
  • 1
  • 1
Dan Orlovsky
  • 1,045
  • 7
  • 18