ASP.NET 母版页

asp.net web forms - 母版页

母版页为您的网站的其他页面提供模版。

母版页

母版页允许您为您的 web 应用程序中的所有页面(或页面组)创建一致的外观和行为。

母版页为其他页面提供模版,带有共享的布局和功能。母版页为内容定义了可被内容页覆盖的占位符。输出结果是母版页和内容页的组合。

内容页包含您想要显示的内容。

当用户请求内容页时,asp.net 会对页面进行合并以生成结合了母版页布局和内容页内容的输出。

母版页实例

<%@ master %>

<html>
<body>
<h1>standard header from masterpage</h1>
<asp:contentplaceholder id="cph1" runat="server">
</asp:contentplaceholder>
</body>
</html>

上面的母版页是一个为其他页面设计的普通 html 模版页。

@ master 指令定义它为一个母版页。

母版页为单独的内容包含占位标签 <asp:contentplaceholder>

id="cph1" 属性标识占位符,在相同母版页中允许多个占位符。

这个母版页被保存为 "master1.master"

lamp 注释:母版页也能够包含代码,允许动态的内容。

内容页实例

<%@ page masterpagefile="master1.master" %>

<asp:content contentplaceholderid="cph1" runat="server">
<h2>individual content</h2>
<p>paragraph 1</p>
<p>paragraph 2</p>
</asp:content>

上面的内容页是站点中独立的内容页中的一个。

@ page 指令定义它为一个标准的内容页。

内容页包含内容标签 <asp:content>,该标签引用了母版页(contentplaceholderid="cph1")。

这个内容页被保存为 "mypage1.aspx"

当用户请求该页面时,asp.net 就会将母版页与内容页进行合并。

点击这里显示 mypage1.aspx

lamp注释:内容文本必须位于 <asp:content> 标签内部。标签外的内容文本是不允许的。

带控件的内容页

<%@ page masterpagefile="master1.master" %>

<asp:content contentplaceholderid="cph1" runat="server">
<h2>yapf</h2>
<form runat="server">
<asp:textbox id="textbox1" runat="server" />
<asp:button id="button1" runat="server" text="button" />
</form>
</asp:content>

上面的内容页演示了如何把 .net 控件插入内容页,就像插入一个普通的页面中。

点击这里显示 mypage2.aspx


相关文章