ASP.NET 母版页(嵌套、访问母版页的控件和属性)


友情链接:母版页的概述与创建

一、母版页的嵌套

嵌套就是大的容器套装小的容器。嵌套母版页就是指创建一个大的母版页,在其中半酣另外一个母版页。利用母版页可以创建组件化的母版页如下图所示:

在这里插入图片描述
下面通过一个例子实现一个简单的嵌套母版页功能:

首先创建一个项目,然后创建一个母版页,名字为MainMaster,接着着创建一个Web窗体母版页(嵌套)注意是嵌套!命名为:SubMaster,然后添加一个包含母版页的Web窗体,命名为:Default.aspx并将其作为SubMaster的内容页。友情提示:快捷键ctrl+shift+a弹出添加新项页面。

创建完成之后,我们的解决方案资源管理器是这个样子的:
在这里插入图片描述
然后开始下一步操作,在MainMaster.Master界面开始写代码:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MainMaster.master.cs" Inherits="_01.MainMaster" %>

<!DOCTYPE html>

<html>
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>主母版页</title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="width:759px;height:634px" cellpadding="0" cellspacing="0" align="center">
                <tr><td style="background-image:url(Image/banner.jpg);width:759px;height:153px"></td></tr>
                <tr><td style="width:759px;height:374px" align="center" valign="middle">
                    <asp:ContentPlaceHolder ID="MainContent" runat="server">

                        </asp:ContentPlaceHolder>
                    </td></tr>
                <tr><td style="background-image:url(Image/3.jpg);width:759px;height:107px"></td></tr>
            </table>
        </div>
    </form>
</body>
</html>

这个是主母版页的代码,在这个代码中在适当的位置占了一个ContentPlaceHolder

子母版页以.master为扩展名,代码包含代码头声明Content控件。不包含主母版页中的<html> <body>等HTML元素,然后开始写代码:

<%@ Master Language="C#" MasterPageFile="~/MainMaster.Master" AutoEventWireup="true" CodeBehind="SubMaster.master.cs" Inherits="first.SubMaster" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table style="background-image:url(Image/2.jpg);width:759px;height:374px;">
        <tr>
            <td align="center" valign="middle">
                <h1>子母版页</h1>
            </td>
            <td align="center" valign="middle" style="width:451px;">
                <asp:ContentPlaceHolder ID ="SubContent" runat="server"></asp:ContentPlaceHolder>
            </td>
        </tr>
    </table>
</asp:Content>

在子母版页的@Master指令中添加了MasterPageFile属性以设置夫母版页路径,从而实现嵌套。

接着就是内容页的代码了,内容页的代码包含两部分,即代码头声明Content控件。由于内容页绑定子母版页,所以代码头中的属性MasterPageFile必须设置为子母版页的路径。代码如下:

<%@ Page Title="" Language="C#" MasterPageFile="~/SubMaster.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="first.Default" %>
<asp:Content ID ="Content2" ContentPlaceHolderID="SubContent" runat="server">
    <table style="width:451px;height:391px">
        <tr>
            <td><h1>内容页</h1></td>
        </tr>
    </table>
</asp:Content>

大功告成!看一下效果:

在这里插入图片描述


二、访问母版页的控件和属性

内容页访问母版页中的属性、方法和控件有一定的限制。对于属性和方法的规则是:如果它们在母版页上被声明为公共成员,则可以访问它们。在引用母版页上的控件时,没有只能访问公共成员的这种限制。

1、使用Master.FindControl()方法访问母版页上的控件

在内容页中,Page对象具有一个公共属性Master,该属性能够实现对相关母版页基类MasterPage的引用。可以用MastetPage对象实现对母版页中各个对象的访问,但由于母版页中的控件时受保护的,不能直接访问,那么就必须使用MasterPage对象中的FindControl方法实现。

接下来看一个例子,内容页中定义一个Label将他赋值为母版页上的Label的内容:

新建网站,添加母版页MasterPage.master,再添加一个Web窗体命名为Default.aspx,作为内容页。然后开始写代码:

MasterPage.master代码(部分):

            <table style="width:759px;height:634px" align="center">
                <tr><td>
                    <asp:Label ID="Mlabel" runat="server" Text="Label"></asp:Label>
                    </td></tr>
                <tr><td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                         </asp:ContentPlaceHolder>
                    </td></tr>
            </table>

MasterPage.master.csPage_Load方法代码:

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Mlabel.Text = DateTime.Now.ToString();
        }

Default.aspx代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Master.FindControl访问母版页控件.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</asp:Content>

Default.aspx.cs代码中的Page_LoadComplete代码:

        protected void Page_LoadComplete(object sender,EventArgs e)
        {
            Label MLable1 = (Label)this.Master.FindControl("Mlabel");
            this.Label2.Text = MLable1.Text;
        }

大功告成!

2、引用@MasterType指令访问母版页上的属性

引用母版页中的属性和方法,需要在内容页中使用@MasterType指令,将内容页的Master属性强类型化,即通过@MasterType指令创建与内容页相关的母版页的强类型引用。在设置@MasterType指令时,必须设置VirtualPath属性用于指定与内容页相关的母版页的存储地址。

看个例子,拿上面的步骤来继续实现:

首先在MasterPage.master.cs代码中,写入以下代码(定义了个属性)

        string mValue = "";
        //定义属性
        public string MValue
        {
            get { return mValue; }
            set { mValue = value; }
        }

然后把<%= this.MValue%>绑定到MasterPage.master代码页可以放在Label控件下面。

然后就是去设置VirtualPath地址了,打开Default.aspx,粘贴以下代码在最上面:

<%@MasterType VirtualPath="~/MasterPage.master" %>

最后在Default.aspx.cs代码中访问一下这个属性:

        protected void Page_Load(object sender, EventArgs e)
        {
            Master.MValue = "Welcome";
        }

大功告成喽~~!!!


发布了177 篇原创文章 · 获赞 282 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/lesileqin/article/details/103653360