大家好,又见面了,我是你们的朋友全栈君。
定义:
ContentPlaceHolder 控件:在 ASP.NET 母版页中定义内容区域。
Content控件:保存文本、标记和服务器控件以呈现给母版页中的 ContentPlaceHolder 控件。
两者关系:
ContentPlaceHolder 控件在母版页中定义相对内容区域,并呈现在内容页中找到的相关的 Content 控件的所有文本、标记和服务器控件。
Content 控件使用其ContentPlaceHolderID 属性与 ContentPlaceHolder 关联。将 ContentPlaceHolderID 属性设置为母版页中相关的 ContentPlaceHolder 控件的ID属性的值。
通 俗的来讲,ContentPlaceHolder 控件是个容器控件,用来存放内容,但是如果它放在母板页中,那么它的内容页就需要使用Content控件来指定ContentPlaceHolder控件 (好像一个指针一样,通过 ContentPlaceHolderID 属性来指定)来放置内容。
注意:
ContentPlaceHolder控件如果放在母版页中,那么它的内容页是通过Content控件来链接,是可编辑的。
但是Content控件如果放在母板页中,那么它的内容页中没有东西来对其进行链接,是不可编辑的。
举例:
母版页一的代码:
- <</SPAN>span style=“font-size:18px;”><</SPAN>span style=“font-size:13px;”><</SPAN>headrunat=“server”>
- <</SPAN>title>演示</</SPAN>title>
- <</SPAN>asp:ContentPlaceHolder id=“head” runat=“server”>
- </</SPAN>asp:ContentPlaceHolder>
- </</SPAN>head>
- <</SPAN>body>
- <</SPAN>form id=“form1” runat=“server”>母版页演示
- <</SPAN>div>
- <</SPAN>asp:ContentPlaceHolder id=“ContentPlaceHolder1” runat=“server”>
- </</SPAN>asp:ContentPlaceHolder>
- </</SPAN>div>
- </</SPAN>form>
- </</SPAN>body></</SPAN>span>
- </</SPAN>span>
母版页一的内容页的代码:
- <</SPAN>span style=“font-size:18px;”><</SPAN>span style=“font-size:13px;”><</SPAN>asp:ContentID=“Content1” ContentPlaceHolderID=“head” Runat=“Server”>
- </</SPAN>asp:Content>
- <</SPAN>asp:Content ID=“Content2” ContentPlaceHolderID=“ContentPlaceHolder1” Runat=“Server”>
- </</SPAN>asp:Content></</SPAN>span>
- </</SPAN>span>
母版页二的代码(二级母版页,继承自母版页一):
- <</SPAN>span style=“font-size:18px;”><</SPAN>span style=“font-size:13px;”><</SPAN>asp:ContentID=“Content1” ContentPlaceHolderID=“head” Runat=“Server”>
- </</SPAN>asp:Content>
- <</SPAN>asp:Content ID=“Content2” ContentPlaceHolderID=“ContentPlaceHolder1” Runat=“Server”>
- </</SPAN>asp:Content></</SPAN>span>
- </</SPAN>span>
母版页二的内容页的代码:没有代码,是空白。
问题:这样的话二级母版页的内容页就不能进行编辑,如何解决呢?
解决:我们只需在二级母版页中添加ContentPlaceHolder控件即可。
举例:上面的母版页二,我们更改后:
- <</SPAN>span style=“font-size:18px;”><</SPAN>span style=“font-size:13px;”><</SPAN>asp:ContentID=“Content1” ContentPlaceHolderID=“head” Runat=“Server”>
- <</SPAN>asp:ContentPlaceHolder ID=“ContentPlaceHolder1” runat=“server”>
- </</SPAN>asp:ContentPlaceHolder>
- </</SPAN>asp:Content></</SPAN>span>
- </</SPAN>span><</SPAN>span style=“font-size:13px;”><</SPAN>asp:Content ID=“Content2”ContentPlaceHolderID=“ContentPlaceHolder1” Runat=“Server”>
- <</SPAN>asp:ContentPlaceHolder ID=“ContentPlaceHolder2” runat=“server”>
- </</SPAN>asp:ContentPlaceHolder>
- </</SPAN>asp:Content>
- </</SPAN>span>
母版页二的内容页变为:
- <</SPAN>span style=“font-size:13px;”><</SPAN>asp:Content ID=“Content1”ContentPlaceHolderID=“ContentPlaceHolder1” Runat=“Server”>
- </</SPAN>asp:Content>
- <</SPAN>asp:Content ID=“Content2” ContentPlaceHolderID=“ContentPlaceHolder2” Runat=“Server”>
- </</SPAN>asp:Content>
- </</SPAN>span>
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158477.html原文链接:https://javaforall.cn