关于ContentPlaceHolder与Content控件

2022-09-14 10:12:14 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

定义:

ContentPlaceHolder 控件:在 ASP.NET 母版页中定义内容区域。

Content控件:保存文本、标记和服务器控件以呈现给母版页中的 ContentPlaceHolder 控件。

两者关系:

ContentPlaceHolder 控件在母版页中定义相对内容区域,并呈现在内容页中找到的相关的 Content 控件的所有文本、标记和服务器控件。

Content 控件使用其ContentPlaceHolderID 属性与 ContentPlaceHolder 关联。将 ContentPlaceHolderID 属性设置为母版页中相关的 ContentPlaceHolder 控件的ID属性的值。

通 俗的来讲,ContentPlaceHolder 控件是个容器控件,用来存放内容,但是如果它放在母板页中,那么它的内容页就需要使用Content控件来指定ContentPlaceHolder控件 (好像一个指针一样,通过 ContentPlaceHolderID 属性来指定)来放置内容。

注意:

ContentPlaceHolder控件如果放在母版页中,那么它的内容页是通过Content控件来链接,是可编辑的。

但是Content控件如果放在母板页中,那么它的内容页中没有东西来对其进行链接,是不可编辑的。

举例:

母版页一的代码:

  1. <</SPAN>span style=“font-size:18px;”><</SPAN>span style=“font-size:13px;”><</SPAN>headrunat=“server”>
  2. <</SPAN>title>演示</</SPAN>title>
  3. <</SPAN>asp:ContentPlaceHolder id=“head” runat=“server”>
  4. </</SPAN>asp:ContentPlaceHolder>
  5. </</SPAN>head>
  6. <</SPAN>body>
  7. <</SPAN>form id=“form1” runat=“server”>母版页演示
  8. <</SPAN>div>
  9. <</SPAN>asp:ContentPlaceHolder id=“ContentPlaceHolder1” runat=“server”>
  10. </</SPAN>asp:ContentPlaceHolder>
  11. </</SPAN>div>
  12. </</SPAN>form>
  13. </</SPAN>body></</SPAN>span>
  14. </</SPAN>span>

母版页一的内容页的代码:

  1. <</SPAN>span style=“font-size:18px;”><</SPAN>span style=“font-size:13px;”><</SPAN>asp:ContentID=“Content1” ContentPlaceHolderID=“head” Runat=“Server”>
  2. </</SPAN>asp:Content>
  3. <</SPAN>asp:Content ID=“Content2” ContentPlaceHolderID=“ContentPlaceHolder1” Runat=“Server”>
  4. </</SPAN>asp:Content></</SPAN>span>
  5. </</SPAN>span>

母版页二的代码(二级母版页,继承自母版页一):

  1. <</SPAN>span style=“font-size:18px;”><</SPAN>span style=“font-size:13px;”><</SPAN>asp:ContentID=“Content1” ContentPlaceHolderID=“head” Runat=“Server”>
  2. </</SPAN>asp:Content>
  3. <</SPAN>asp:Content ID=“Content2” ContentPlaceHolderID=“ContentPlaceHolder1” Runat=“Server”>
  4. </</SPAN>asp:Content></</SPAN>span>
  5. </</SPAN>span>

母版页二的内容页的代码:没有代码,是空白。

问题:这样的话二级母版页的内容页就不能进行编辑,如何解决呢?

解决:我们只需在二级母版页中添加ContentPlaceHolder控件即可。

举例:上面的母版页二,我们更改后:

  1. <</SPAN>span style=“font-size:18px;”><</SPAN>span style=“font-size:13px;”><</SPAN>asp:ContentID=“Content1” ContentPlaceHolderID=“head” Runat=“Server”>
  2. <</SPAN>asp:ContentPlaceHolder ID=“ContentPlaceHolder1” runat=“server”>
  3. </</SPAN>asp:ContentPlaceHolder>
  4. </</SPAN>asp:Content></</SPAN>span>
  5. </</SPAN>span><</SPAN>span style=“font-size:13px;”><</SPAN>asp:Content ID=“Content2”ContentPlaceHolderID=“ContentPlaceHolder1” Runat=“Server”>
  6. <</SPAN>asp:ContentPlaceHolder ID=“ContentPlaceHolder2” runat=“server”>
  7. </</SPAN>asp:ContentPlaceHolder>
  8. </</SPAN>asp:Content>
  9. </</SPAN>span>

母版页二的内容页变为:

  1. <</SPAN>span style=“font-size:13px;”><</SPAN>asp:Content ID=“Content1”ContentPlaceHolderID=“ContentPlaceHolder1” Runat=“Server”>
  2. </</SPAN>asp:Content>
  3. <</SPAN>asp:Content ID=“Content2” ContentPlaceHolderID=“ContentPlaceHolder2” Runat=“Server”>
  4. </</SPAN>asp:Content>
  5. </</SPAN>span>

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158477.html原文链接:https://javaforall.cn

0 人点赞