代码改变世界

【Asp.net从零开始】:使用母版页(Master Pages) (二)

2012-08-09 10:19  ATP_  阅读(6050)  评论(0编辑  收藏  举报

【Asp.net从零开始】:使用母版页(Master Pages) (一):http://www.cnblogs.com/VortexPiggy/archive/2012/08/09/2629623.html

文章多参考MSDN,以及Web Applications Development with Microsoft .NET Framework 4 (MCTS的英文教材,所以有些许翻译可能不是特别准确)

   一.在内容页中引用母版页中的属性,方法以及控件   

内容页中的代码可以引用母版页上的成员,包括母版页上的任何公共属性或方法以及任何控件。

1.在母版页的.aspx文件中创建公共属性

2.在内容页的@Page指令下方,添加@MasterType属性,将内容页中的Master属性绑定至需要引用的母版页

<%@ MasterType virtualpath="~/Master1.master" %>

3.在内容页中通过Mster.<属性名>来引用母版页成员。

代码示例:

//在MasterPage中添加属性
 public  String SharedInfo 
    {
        get{return Session["SharedInfo"] as string;}
        set { Session["SharedInfo"] = value; }
    }
//在内容页中添加好@MasterType VirtualPath
 protected void Page_Load(object sender, EventArgs e)
    {
        Master.SharedInfo = "sharedinfo";    
    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        lb.Text = Master.SharedInfo;
    }

  引用母版页的控件:使用Master.FindControl

//在母版页中设置一个label控件
<asp:Label Text="这是一个用于测试的标签" Id="lb" runat="server" />
//在内容页中调用
 Label content_lb = (Label)Master.FindControl("lb");
        content_lb.Text = "测试成功";

  

   二.嵌套母版页   

说白了就是在次母版页中,既有对应主母版页ContentPlaceHolder的Content,也拥有自己为内容页所留下的ContentPlaceHolder

主母版页:

Site.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>重新过一遍ASP.NET 2.0(C#)</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
    </form>
</body>
</html>
次母版页:

MasterPage/MasterPage.master

<%@ Master Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="MasterPage.master.cs" Inherits="MasterPage_MasterPage" 
%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <p>
        我是一个嵌套母版页
    </p>
    <p>
        母版页中的内容
        <asp:DropDownList ID="ddlMaster" runat="server" DataSourceID="XmlDataSource1" DataTextField="text"
            DataValueField="value" AutoPostBack="True" OnSelectedIndexChanged="ddlMaster_SelectedIndexChanged">
        </asp:DropDownList><asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Config/DropDownListData.xml">
        </asp:XmlDataSource>
    </p>
    <p>
        内容页中的内容
        <asp:ContentPlaceHolder ID="cph" runat="Server" />
    </p>
</asp:Content>

 

   三.动态编程改变母版页(Dynamically Changing Master Page)   

直接上示例:

 void Page_PreInit(Object sender, EventArgs e)
    {
        //判断Session不为零时,通过对MasterPageFile属性进行设置来改变母版页
        if (Session["masterPage"] != null)
            MasterPageFile = (string)Session["masterPage"];
    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        //一个母版页切换的逻辑控制
        if ((string)Session["masterPage"] == "~/MasterPage.master")
        {
            Session["masterPage"] = "~/MasterPage1.master";
            //当改变完Session内容后需要刷新页面
            Response.Redirect(Request.Url.ToString());
            //另一种刷新页面的方式
            //Server.Transfer(Request.Path);
        }
        else
        {
            Session["masterPage"] = "~/MasterPage.master";
            Response.Redirect(Request.Url.ToString());
        }
    }