有没有一种简单的方法可以让多个 ContentPlaceHolder 控件彼此独立更新?
这是一个非常简单的 ASP.NET 母版页示例。母版页显示 4 个超链接并具有两个 ContentPlaceholder 控件。前两个链接指向将在 ContentPlaceHolder1 中显示的内容页面,后两个链接指向将在 ContentPlaceHolder2 中显示的内容页面。
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="ProofOfConcept.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Demo site
</h1>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/HeadContent1.aspx">Head Content 1 in ContentPlaceHolder1</asp:HyperLink>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/HeadContent2.aspx">Head Content 2 in ContentPlaceHolder1</asp:HyperLink>
<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/MainContent1.aspx">Main Content 1 in ContentPlaceHolder2</asp:HyperLink>
<asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/MainContent2.aspx">Main Content 2 in ContentPlaceHolder2</asp:HyperLink>
<br />
<div style="border: 1px dotted blue;">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
This is default text for ContentPlaceholder1
</asp:ContentPlaceHolder>
</div>
<br />
<div style="border: 1px dotted red;">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
This is default text for ContentPlaceholder2
</asp:ContentPlaceHolder>
</div>
</div>
</form>
</body>
</html>
四个内容页面本身都如下所示:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.master" AutoEventWireup="true" CodeBehind="MainContent1.aspx.cs" Inherits="ProofOfConcept.MainContent1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceholder2" runat="server">
This is text from MainContent1 in ContentPlaceholder2
</asp:Content>
...ContentPlaceholder ID 设置为适当的。换句话说,每个内容页仅包含一个链接到母版页上的 ContentPlaceHolders 之一的内容控件。
例如,如果我构建网站并加载 HeadContent1.aspx(第一个链接),则仅显示 HeadContent1 中的内容(显然还加上母版页中的标记)。如果我单击第三个链接,则会显示第二个 ContentPlaceHolder 中的内容,但第一个占位符将恢复为其默认标记。
这种行为似乎都是按照设计的,并且在许多情况下无疑非常有用,但我想做的是让两个 ContentPlaceholder 彼此独立刷新。或多或少像老式 HTML 框架一样工作?这是可能的,还是我应该使用其他一些控件(或不同的设置而不是主/内容)?
This is a very simple ASP.NET master page example. The master page displays 4 hyperlinks and has two ContentPlaceholder controls. The first two links are to content pages that will display in ContentPlaceHolder1, the second two links are to content pages that will display in ContentPlaceHolder2.
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="ProofOfConcept.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Demo site
</h1>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/HeadContent1.aspx">Head Content 1 in ContentPlaceHolder1</asp:HyperLink>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/HeadContent2.aspx">Head Content 2 in ContentPlaceHolder1</asp:HyperLink>
<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/MainContent1.aspx">Main Content 1 in ContentPlaceHolder2</asp:HyperLink>
<asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/MainContent2.aspx">Main Content 2 in ContentPlaceHolder2</asp:HyperLink>
<br />
<div style="border: 1px dotted blue;">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
This is default text for ContentPlaceholder1
</asp:ContentPlaceHolder>
</div>
<br />
<div style="border: 1px dotted red;">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
This is default text for ContentPlaceholder2
</asp:ContentPlaceHolder>
</div>
</div>
</form>
</body>
</html>
The four content pages themselves all look like this:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.master" AutoEventWireup="true" CodeBehind="MainContent1.aspx.cs" Inherits="ProofOfConcept.MainContent1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceholder2" runat="server">
This is text from MainContent1 in ContentPlaceholder2
</asp:Content>
...with the ContentPlaceholder ID setas appropriate. In other words, each content page only contains one Content control linked to the one of the ContentPlaceHolders on the Master page.
If I build the site and load HeadContent1.aspx (the first link), for instance, only the content from HeadContent1 is displayed (plus markup from the master page, obviously). If I click the third link, content in the second ContentPlaceHolder is displayed, but the first Placeholder reverts to its default markup.
This behaviour all appears to be as designed and is undoubtedly very useful in many scenarios, but what I'd like to do is have the two ContentPlaceholders refresh independently of each other. To work like old-fashioned HTML frames, more or less? Is this possible, or should I be using some other control (or a different setup instead of Master/Content)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说你想根据你的情况使用 iframe。母版页无法按照您期望的方式运行。
I would say you want to use iframes for your situation. Master pages don't function the way you are looking for.
我同意迈克的观点。你要绕很远的路。为了使您的示例能够使用母版页,您将需要 16 个不同的内容页面。每个页面都需要填充 PlaceHolder1 和 PlaceHolder2 的内容,具体取决于单击的链接以及单击的顺序。
IFrame 是处理此类问题的更好方法,但我首先建议您检查您的设计,以确定是否有一种方法可以简化它。
I agree with Mike. You are going the long way around. In order for your example to work with Master Pages, you will need 16 different content pages. Each page would need to fill the content for PlaceHolder1 and PlaceHolder2 depending on which links were clicked and in what order.
An IFrame would be a better way to handle something like this, but I would first recommend you review your design to determine if there is a way that it can be simplified.