views:

225

answers:

3

the function

private void SetUpMasterPage(){
    this.MasterPageFile = "~/MasterPages/NestedMasterPageTest2.Master"; 
}

is called on the OnPreInit... This works when the masterpagefile is the base masterpage... But how are we going to make it work for the nested masterpage?

we actually tried

this.Master.MasterPageFile = "~/MasterPages/Base.Master";

but it throws an exception... =(


So, to make things clearer, on the aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test._Default"%>
<asp:Content ID="testContent" ContentPlaceHolderID="body" runat="server">
This is a test!
</asp:Content>

on the base.Master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Base.master.cs" Inherits="Test.Base" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>The title</title>
    <asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="body" runat="server">
        </asp:ContentPlaceHolder>
    </div>
</form>
</body>
</html>

and on the default.aspx.cs

protected override void OnPreInit(EventArgs e){
    this.MasterPageFile = "~/MasterPages/Base.Master";
}

it is working...


but when I use a nested master page

<%@ Master Language="C#" MasterPageFile="~/MasterPages/Base.Master" AutoEventWireup="true" CodeBehind="NestedMasterPageTest2.master.cs" Inherits="Test.MasterPages.NestedMasterPageTest2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="body" runat="server">
</asp:Content>

then changed the default.aspx.cs

protected override void OnPreInit(EventArgs e){
    this.MasterPageFile ="~/MasterPages/NestedMasterPageTest2.Master";
}

and I changed the default.aspx

<asp:Content ID="testContent" ContentPlaceHolderID="Content2" runat="server">
This is a test which uses a nested master page!
</asp:Content>

it returns an error that says Cannot find ContentPlaceHolder 'Content2' in the master page '/MasterPages/NestedMasterPageTest2.Master', verify content control's ContentPlaceHolderID attribute in the content page. But Content2 is on NestedMasterPageTest2. What really must have happened here?

A: 

Similar Question Here at A S Overflow

Harryboy
i have already read that before i posted here...
Jronny
So it did not work ?
ram
@Ram: It's a different issue he has there.
Jronny
+1  A: 

Your nested master page has Content control but no ContentPlaceHolder control. Try adding it:

<asp:ContentPlaceHolder ID="cplh" runat="server">
</asp:ContentPlaceHolder>

Then, in Default.aspx:

<asp:Content ID="cnt1" ContentPlaceHolderID="cplh" runat="server">
  This is a test which uses a nested master page!
</asp:Content>
Pavel Chuchuva
yeah, I get it now. I'll try this then...
Jronny
+1  A: 

You need to add an <asp:ContentPlaceholder> tag to your nested master page:

<asp:Content ID="basebody" ContentPlaceHolderID="body" runat="server">
    <asp:ContentPlaceHolder ID="Content2" runat="server">
    </asp:ContentPlaceHolder>
</asp:Content>
RickNZ