zoukankan      html  css  js  c++  java
  • ASP.NET 2.0 之 Master Page 学习笔记

    如何使用 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 已经过期,则会引发一个错误。
        所以实际上只能对子页面指定缓存。

  • 相关阅读:
    JAVA文件操作类和文件夹的操作代码示例
    java去除表达符号的正则表达式
    正则表达式以过滤特殊字符
    eclipse与myeclipse恢复已删除的文件和代码
    Windows 2003 Server R2 x64 IIS6.0 eWebEditor无法显示的问题
    获得每日,每周,每月的0点和24点的时间戳
    Access查询时间段 .
    java连接Access数据库的两种方法
    移动App专项测试
    linux性能评估-内存基础理解篇
  • 原文地址:https://www.cnblogs.com/RChen/p/356085.html
Copyright © 2011-2022 走看看