2016-03-17 46 views
0

我正在嘗試用兩個ContentPlaceHolder部分創建一個Main.master頁面。 當我加載默認頁面,它只呈現ContentPlaceHolder1,我不得不實際加載Second.aspx看到第二個ContentPlaceHolder。爲什麼?從單獨文件加載ContentPlaceHolders

在我Main.master我:

<%@ 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> 

此外,我已經創建了兩個附加頁的Default.aspx和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> 

其他頁面

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> 

它只渲染第一個PlaceHolder,我怎樣才能擁有單獨的內容文件並且同時具有渲染d在同一頁上?

enter image description here

回答

0

你要問自己:如果您導航到Default.aspx的,如何將你的應用程序知道搶在Second.aspx的內容?簡單地說:它不會。

首先,這裏是ContentPlaceHolder上的MSDN

您可以根據需要在您的母版頁上添加儘可能多的ContentPlaceHolders,每個頁面可以渲染到這些內容區域或不渲染。

所以你的Default.aspx可以關注一下:

<%@ 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> 

,你會得到你正在尋找的結果。

現在,你可以渲染一個OUTSIDE html文件INTO其中一個內容佔位符(一種方式是JavaScript) - 但(如果我錯了,請別人糾正我),不會有一個用ASPX網頁來做到這一點,無論是否隱藏。

談談你的問題:

我怎麼能有單獨的內容文件,並已在這兩個呈現在同一頁面上?

你可能想看看這個: How to include a partial view inside a webform