惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
博客园 - 司徒正美
Last Week in AI
Last Week in AI
爱范儿
爱范儿
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
量子位
V
V2EX
博客园 - 叶小钗
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog

博客园 - blue1000

C#批量生成大数据量无重复随机数据的另类高效实现 [转载]安装网银安全控件 近期学习SQL Server,收藏的几个学习教程网址备忘 DataColumn.Caption属性应用到DataGridView.HeaderText的方法 飞梭TXT文本数据处理百宝箱 - 极速、高效、灵活 中国父母十个错误教育观念[转] asp.net根据域名查ip C#版 - blue1000 - 博客园 asp.net 2.0教程 登录系列控件 asp.net 2.0教程 成员资格和角色管理 asp.net 2.0教程 Web部件WebPart asp.net 2.0教程 个性化用户配置 asp.net 2.0教程 数据缓存 asp.net 2.0教程 数据绑定控件 asp.net 2.0教程 数据源控件2 asp.net 2.0教程 数据源控件 asp.net 2.0教程 其它服务器控件 asp.net 2.0教程 网站导航控件 asp.net 2.0教程 主题和皮肤 asp.net 2.0教程 C# 2.0匿名方法、迭代器、局部类
asp.net 2.0教程 母版页
blue1000 · 2007-10-08 · via 博客园 - blue1000

尊重作者,请保留 www.it55.com 链接字样。

过完五一了,一身的臭汗,想洗个澡先。但最后还是决定先把今天的课程写了再作其他事情。
我们做网站的时候经常会出现这种情况:每个页面都要有头部和尾部,而这些头部或尾部大多数时间都是一模一样的,重复性的劳动让我们显得很无奈。asp程序员通过include来解决这个问题,asp.net 1.X程序员通过自定义控件来实现公共部分的重用。但这些方法都有无法回避的弊端,asp.net提供给我们了另一种新的解决方案,那就是母版技术。
1、简单母版应用
在当前项目上右键单击>>>添加新项


在弹出的“添加新项”对话框的模板选项中,选择母版页,点击“添加”

vs已经将一个简单的母版页添加到当前项目中。
对母版页面进行简单修改,最终代码如下:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <p>我是母版页内容</p>
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

大家注意看代码的蓝色部分。

接下来我们向当前项目中添加web窗体文件:
在当前项目上右键单击>>>添加新项。
在弹出的“添加新项”对话框的模板选项中,选择web窗体,并同时选中“选择母版页”,点击“添加”:

系统接着会弹出“选择母版页”配置框,选择对应项目下的对应母版,单击“确定”:

系统便自动生成内容页,代码经简单修改,代码如下:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
我是内容页部分。
</asp:Content>

大家注意看代码的蓝色部分。
运行之后,大家会看到:

当我们请求内容页Default.aspx时,系统自动将母版文件内容应用至内容页中,并把最终处理结果展示给我们。asp.net 2.0究竟是怎么实现母版页和内容页之间的整合的呢?下面是一个简单的过程描述:
用户通过内容页的URl请求这个内容页面 >>> 系统获取该内容页后,通过读取@page指令中的MasterPageFile属性获取对应的母版页 >>> 系统将母版页合并到内容页的控件树中。 >>> 系统根据母版页中的"contentplaceholder"控件的ID和内容页Content控件的"ContentPlaceHolderID"属性值,将各个Content控件的内容合并到母版页中相应的ContentPlaceHolder控件中 >>> 系统将合并之后的结果页面发送给客户端。

2、嵌套母版应用
嵌套母版页其实是母版页功能的高级应用,就是在大的母版页中包含一个小的母版页。这里需要注意的是,不管母版页有无嵌套、或者有几个嵌套,整个页面构架中必须至少包含一个内容页,因为母版页本身是不能被用户访问到的。
以下是嵌套母版页应用的简略代码:
大母版页BigMasterPage.master:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MainMasterPage.master.cs" Inherits="MainMasterPage" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>大母版页内容</div>
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>#p#分页标题#e#
    </form>
</body>
</html>

大母版和简单应用时的一样,没有什么变动.

小母版页SmallMasterPage.master:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="SmallMasterPage.master.cs" Inherits="SmallMasterPage" MasterPageFile="~/MainMasterPage.master"%>
    <asp:Content ID="SubContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <div>小母版内容</div>
        <div>
            <asp:contentplaceholder id="ContentPlaceHolder2" runat="server">
            </asp:contentplaceholder>
        </div>
    </asp:Content>

这里看来小母版其实是一个带有母版标记的特殊内容页而已,Content控件之外只允许存在文件头,和部分程序,拒绝存在html代码等.

内容页:Content.aspx:

<%@ Page Language="C#" MasterPageFile="~/SmallMasterPage.master" AutoEventWireup="true" CodeFile="Content.aspx.cs" Inherits="Content" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
内容页内容
</asp:Content>

和简单应用时也一样,不过这里选用的母版是SmallMasterPage.master.
大家着重看蓝色部分哦.运行结果:


3、访问母版页控件及属性
在进行程序设计的时候,我们可能需要在内容页中对母版页进行操控.要访问母版页的的控件或属性有以下几种方法:

一、使用FindControl方法获取母版页控件的引用
利用内容页page对象的Master公共属性,我们可以实现对关联母版页的引用。进而使用母版页的FindControl方法来实现对母版页控件的访问。
母版页MasterPage.master:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage1.master.cs" Inherits="MasterPage1" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label runat="server" ID="masterlabel">母版页的内容</asp:Label>
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

内容页Content1.aspx:

<%@ Page Language="C#" MasterPageFile="~/MasterPage1.master" AutoEventWireup="true" CodeFile="content1.aspx.cs" Inherits="content1" Title="Untitled Page" %>
<script runat="server">
    void Page_LoadComplete(Object sender, EventArgs e)
    {
        contentlabel.Text = (Master.FindControl("masterlabel") as Label).Text;
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="contentlabel" runat="server">这里将显示母版页masterlabel控件的内容。</asp:Label>
</asp:Content>
其中,“Page_LoadComplete”是内容页面加载完成时触发的一个事件。
运行结果:

二、使用MasterType指令获取母版页控件的引用
相对于上面的FindControl方法而言,MasterType显得很直接。通过使用MasterType,可以创建与母版页的强类型引用。

将FindControl方法例子中的MasterPage.master更改如下:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage1.master.cs" Inherits="MasterPage1" %>
<script runat="server">
    public Label MasterPageLabel//注意:将母版页label控件强类型化,方便内容页访问。对母版页属性的访问也使用该方法。
    {
        get#p#分页标题#e#
        {
            return masterlabel;
        }
        set
        {
            masterlabel = value;
        }
    }
</script>
<!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>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label runat="server" ID="masterlabel">母版页的内容</asp:Label>
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

将FindControl方法例子中的Content1.aspx更改如下:

<%@ Page Language="C#" MasterPageFile="~/MasterPage1.master" AutoEventWireup="true" CodeFile="content1.aspx.cs" Inherits="content1" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/MasterPage1.master" %>
<script runat="server">
    new void Page_Load(Object sender, EventArgs e)
    {
        contentlabel.Text = Master.MasterPageLabel.Text;
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="contentlabel" runat="server">这里将显示母版页masterlabel控件的内容。</asp:Label>
</asp:Content>

4、母版页应用范围配置
以上实例都是页面级母版页应用,只要在对应内容页页面头部声明或设置即可。
如果是程序级母版页应用,应在Web.config中作以下设置:
<configuration>
   <system.web>
     <pages MasterPageFile="~/MasterPage.master" />
   </system.web>
</configuration>
屏蔽某个文件夹使用该母版方案,可以通过在该文件夹下放置不同的web.config文件来实现。
屏蔽某个文件使用该母版方案,可以通过设置.aspx页面头部来实现:
<%@ Page Language="C#" MasterPageFile="~/OtherMasterPage.master" %>
如果不想使用母版,可以把MasterPageFile属性值留空即可。

下节内容:asp.net 2.0新特性:主题和皮肤