ASP.NET---母版页

母版页的功能为提高工作效率、降低开发和维护强度。母版页应用于网站标志、广告条、导航条、版权声明等内容。

常见母板页代码结构如下所示:

<%@ MasterLanguage="C#"AutoEventWireup="true"CodeBehind="Site1.master.cs"Inherits="WebApplication1.Site1"%>

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <asp:ContentPlaceHolderID="head"runat="server">

    </asp:ContentPlaceHolder>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ContentPlaceHolderID="ContentPlaceHolder1"runat="server">

        </asp:ContentPlaceHolder>

    </form>

</body>

</html>

母板页优点:

    1)有利于站点修改和维护,降低开发人员的工作强度。

    2)提供高效的内容整合能力。

    3)有利于实现页面布局。

    4)提供一种便于利用的对象模型。

创建母板页

创建一个母板页Site1.Master和一个内容页Main.Aspx。

                 母板页包含页头和页尾等内容,内容页中包含内容1和内容2

  •  创建母板页

        对母板页代码进行编辑:

       

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

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <style>

        *

        {

            margin: 0;

            padding: 0;

        }

    </style>

    <asp:ContentPlaceHolderID="head"runat="server">

    </asp:ContentPlaceHolder>

</head>

<body>

    <form id="form1" runat="server">

    <div style="width: 100%; height: 100px; background-color:Aqua">

        handle

        <asp:Label ID="lblTime"runat="server"Text="当前页面:"></asp:Label>

    </div>

    <div style="width: 1200px; margin: 0 auto;">

        <asp:ContentPlaceHolderID="ContentPlaceHolder1"runat="server">

        </asp:ContentPlaceHolder>

    </div>

    <div style="width: 100%; height: 100px; background-color:Fuchsia">

        Footer

    </div>

    </form>

</body>

</html>

  • 创建内容页

内容页Main.aspx的源代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
    CodeBehind="Main.aspx.cs" Inherits="WebApplication1.Main" %>

<%@ MasterType VirtualPath="~/Site1.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div style="width: 100%; height: 100px; background-color: Purple">
        Main内容页
    </div>
</asp:Content>

如图显示Main.aspx设计视图

  • 访问母板页

内容包括:使用FindControl,获取母板页控件的实现方法

使用MasterType指令,获取母板页控件引用的实现方法,使用MasterType指令,获取母板页简单自定义属性的实现方法。

1)使用FindControl,获取母板页控件引用

从内容页获取母板页控件引用的方法

母板页Site1.Master源代码如下:

<%@ MasterLanguage="C#"AutoEventWireup="true"CodeBehind="Site1.master.cs"Inherits="WebApplication1.Site1"%>

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <style>

        *

        {

            margin: 0;

            padding: 0;

        }

    </style>

    <asp:ContentPlaceHolderID="head"runat="server">

    </asp:ContentPlaceHolder>

</head>

<body>

    <form id="form1" runat="server">

    <div style="width: 100%; height: 100px; background-color:Aqua">

        handle

        <asp:Label ID="lblTime"runat="server"Text="当前页面:"></asp:Label>

    </div>

    <div style="width: 1200px; margin: 0 auto;">

        <asp:ContentPlaceHolderID="ContentPlaceHolder1"runat="server">

        </asp:ContentPlaceHolder>

    </div>

    <div style="width: 100%; height: 100px; background-color:Fuchsia">

        Footer

    </div>

    </form>

</body>

</html>

内容页Main.aspx文件代码

using System;

usingSystem.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

usingSystem.Web.UI.WebControls;

 

namespace WebApplication1

{

    public partial class Main :System.Web.UI.Page

    {

        protected voidPage_Load(object sender, EventArgs e)

        {
            Label lb = Master.FindControl("lblTime") asLabel;

           lb.Text += "Main.aspx";

        }

    }

}

2)使用MasterType指令,获取母板页控件引用

母板页后台代码:

using System;

usingSystem.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace WebApplication1

{

    public partial class Site1 :System.Web.UI.MasterPage

    {

        protected voidPage_Load(object sender, EventArgs e)

        {

 

        }

        public string Msg

        {

            get

            {

               return lblTime.Text;

            }

            set

            {

               lblTime.Text += value;

            }

        }

    }

}

             内容源代码;

using System;

usingSystem.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

usingSystem.Web.UI.WebControls;

 

namespace WebApplication1

{

    public partial class Main :System.Web.UI.Page

    {

        protected voidPage_Load(object sender, EventArgs e)

        {

           Master.Msg = "Main.aspx";

        }

    }

}

两种方法运行后如图所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值