zoukankan      html  css  js  c++  java
  • 如何使用 MasterPage

    MasterPageFile母版页  

     

    刚开始学,什么都不懂,看到了这段代码,才促使自己去研究MasterPageFile到底是什么含义。
    <%@ Page Language="C#" MasterPageFile="~/MasterPages/Master1.master" Title="Content Page"%>


    MasterPageFile其实是定义一个了外围的框架的页面,并且在里面设定了具体内容存放的位置。举例来说,MasterPageFile
    就相当于相框的功能。网站的真正具体页面实际是由MasterPageFile(相框)和内容页面(照片)组成。

    最常见的应用就是网站的顶部导航栏和底部的CopyRight声明栏。可以把顶部导航栏和底部的CopyRight声明栏都放到MasterPageFile里,然后声明中间是放内容页面的。 这样网站的的每个页面都有统一的顶部导航栏和底部的CopyRight声明栏,只是中间的内容页面不同而已。

    母版页为具有扩展名 .master(如 Main.master)的 ASP.NET 文件,由特殊的 @ Master 指令识别如下:
    <%@ Master Language="C#" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

    如何使用 MasterPage(注意母板页和子页面的执行顺序)

    1. 创建 MasterPage,后缀名 .master, 如 x.master.     其中用 <asp:ContentPlaceHolder /> 定义空位。如:

        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" Runat="Server">     </asp:ContentPlaceHolder>
    2. 创建内容页面。     在 NewItem 对话框里选择 "select master page", 选择上一步创建的 MasterPage.     产生的代码里, MasterPageFile 属性指定了 MasterPage 的位置:

        <%@ Page Language="VB" MasterPageFile="~/x.master" Title="无标题页面" %>

        页面里用 <asp:Content /> 来添加内容到对应的空位:

        <asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1" Runat="Server">         内容     </asp:Content/>

        内容页面没有 <form id="form1" runat="server">

    3. 利用 MasterPage 可以使用多种语言来编写一个页面的各个部分。

    4. 除了在 <%@ Page %> 里面指定 MasterPage, 也可以在 web.config 指定:

        <configuration>         <system.web>             <pages masterPageFile="~/x.master" />         </system.web>     </configuration>

        这样定义后,如果创建 Page 时选择了 master page, 则在 <%@ Page %> 里面不需要指定即可使用该 MasterPage.     其他页面要使用不同的 MasterPage 的话,只要用第一种方法在 Page directive 里面明确的覆盖 web.config 里的设置即可。

        可以仅对一组 pages 指定 MasterPage. 下例利用 web.config 的 location 元素,设定了 Admin 目录下的页面采用的不同的 MasterPage.

        <configuration>         <location path="Admin">             <system.web>                 <pages masterPageFile="~/y.master" />             </system.web>         </location>     </configuration>

    5. 在内容页面如何设定 Page 的 Title ?

        默认情况下,Title 在 MasterPage 中指定后,其他具体页面就都使用这个 Title.     在具体页面,可以有两个办法修改 Title:          a. <%@ Page Title="test" %>

        b. 代码中:

            protected void Page_LoadComplete(object sender, EventArgs e)         {             Master.Page.Title = "Hello";         }

         6. 访问 MasterPage 中的属性和控件。

        用 Master 属性来访问。

        a. 假设 MasterPage 中有一个 Label1, 那么在内容页面可以这样:

            protected void Page_LoadComplete(object sender, EventArgs e)         {             string text = (Master.FindControl("Label1") as Label).Text;         }

            页面加载的次序:                  要获取在 MasterPage 的 Page_Load 里面设定的值,必须在内容页面的 Page_LoadComplete 中来写。

            前面提到的 FindControl() 方法来查找 MasterPage 中的控件,是一种后期绑定的做法,一般是不安全的。因为这取决于 MasterPage 中是否存在这个 tag,如果被删除了,则会导致错误。         比较好的做法是,在 MasterPage 中用属性封装对他的控件的访问;如果用 FindControl(), 则总是检查其结果是否为 null.

    7. 指定 MasterPage 中的默认内容

        直接在 <asp:ControlPlaceHolder /> 标签之间指定即可。     如果子页面不重新指定,则会采用该默认内容。

    8. 编程的方式指定 Master Page

        protected void Page_PreInit(object sender, EventArgs e)     {         Page.MasterPageFile = "~/x.master";     }

    9. 嵌套的 Master Page

        Master Page 可以继承自更高层次的 Master Page. 但是在 VS2005 中创建这种子 Master Page 的时候,不会有默认的支持。     假设有了一个 A.master,     我们现在先创建一个普通的 B.master,     然后删除其中除了 Page directive 的其他部分。     把 Page Directive 修改为如下,并加入自己要定义的 PlaceHolder:

        <%@ Master MasterPageFile="~/A.master" %>

        <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="server">         Hello!         <asp:ContentPlaceHolder ID="ContentPlaceHolder2" Runat="server">         </asp:ContentPlaceHolder>     </asp:Content>

        用嵌套的模板产生的子页面将不能采用 VS2005 的 design 模式。

    10. 容器特定的 Master Pages

        为了能兼容不同的浏览器,asp.net 2.0 支持多个 Master Page. 在运行时将自动加载合适的 Master Page.

        语法如下:

        <%@ Page Language="VB" MasterPageFile="~/Abc.master"         Mozilla:MasterPageFile="~/AbcMozilla.master"         Opera:MasterPageFile="~/AbcMozilla.master" %>

    11. 页面请求的次序

        当用户请求一个用 Master Page 构建的页面时,各种事件发生的次序如下:

        Master Page 子控件初始化;     内容页面子控件初始化;     Master Page 初始化;     内容页面初始化;     内容页面 Page_Load;     Master Page 的 Page_Load;     Master Page 子控件加载;     内容页面子控件加载;

             注意点:          因为内容页面的 Page_Load 先于 Master Page 的 Page_Load,所以,如果要访问 Master Page 里的服务器控件,则必须在内容页面的 Page_LoadComplete 方法里书写代码。

    12. 使用缓存

        只有在内容页面才可以使用如下的 directive 指定缓存:

        <%@ OutputCache Duration="10" Varybyparam="None" %>

        (这个指令让服务器在内存里缓存该页面 10 秒钟)

        如果对 Master Page 指定该指令,本身并不会引发错误。但是当他的子页面下一次来获取其 Master Page 的时候,如果这时 Master Page 已经过期,则会引发一个错误。     所以实际上只能对子页面指定缓存。

  • 相关阅读:
    第二次作业循环语句
    c语言01次作业分支,顺序结构
    PAT 1027. Colors in Mars
    PAT 1026 Table Tennis
    PAT 1035 Password
    PAT 1038. Recover the Smallest Number
    PAT 1028 List Sorting (25)
    PAT 1041 Be Unique (20)
    PAT 1025 PAT Ranking
    1037. Magic Coupon
  • 原文地址:https://www.cnblogs.com/fanyf/p/MasterPage.html
Copyright © 2011-2022 走看看