isyeo

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

使用 ASP.NET 母版页可以为应用程序中的页创建一致的布局。 单个母版页可以为应用程序中的所有页(或一组页)定义所需的外观和标准行为。 然后可以创建包含要显示的内容的各个内容页。 当用户请求内容页时,这些内容页将与母版页合并,从而产生将母版页的布局与内容页中的内容组合在一起的输出。


 <%@ Master Language="C#" %>


 

除@Master指令外,母版页还包含页的所有顶级HTML元素,如html、head和form。除会在所有页上显示的静态文本和控件外,母版页还包括一个或多个ContentPlaceHolder控件。这些占位符控件定义可替换内容出现的区域。接着在内容页中定义可替换内容。通过创建各个内容页来定义母版页的占位符控件的内容,这些内容页为绑定到特定母版页的 ASP.NET 页(.aspx 文件以及可选的代码隐藏文件)。定义 ContentPlaceHolder 控件后,母版页可能看起来类似于下面这样。


<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Normal.master.cs" Inherits="WebSite.Template.Normal" %>
<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>
        <asp:contentplaceholder id="ContentTitle" runat="server"></asp:contentplaceholder>
    </title>
    <link href="http://test/template/public_global.css" rel="stylesheet" type="text/css" />
    <script language="JavaScript" src="/js/public.js" type="text/javascript"></script>
</head>
<body id="theBody">
    <form id="form1" runat="server">
        <div>
                <a href="<%=baseUrl %>/Home.asp">返回首页</a>
                <asp:LinkButton runat="server" ID="lbLogout" OnClick="lbLogout_Click"></asp:LinkButton>
        </div>
        <div>
                <asp:ContentPlaceHolder ID="ContentMain" runat="server">
                </asp:ContentPlaceHolder>
        </div>
        <div>
               Copyright<span>&copy;</span>
        </div>
    </form>
</body>
</html>


 

通过包含指向要使用的母版页的MasterPageFile 特性,在内容页的 @ Page 指令中建立绑定。 例如,一个内容页可能包含下面的 @ Page 指令,该指令将该内容页绑定到 Normal.master 页


<%@ Page Language="C#" MasterPageFile="~/Template/Normal.Master""%>     //波浪线是相对路径,直接从跟目录下查找文件
<asp:Content ID="Content1" ContentPlaceHolderID="ContentTitle" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentMain" runat="server">
  页面内容
</asp:Content>


 

详细内容请参照 http://msdn.microsoft.com/zh-cn/library/wtxbf3hh.aspx

posted on 2012-05-15 17:07  isyeo  阅读(302)  评论(2)    收藏  举报