如何使用 MasterPage

1. 建立 MasterPage,后缀名 .master, 如 x.master.web

    其中用 <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">asp.net


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


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

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

    这样定义后,若是建立 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 已通过期,则会引起一个错误。    因此实际上只能对子页面指定缓存。

相关文章
相关标签/搜索