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

推荐订阅源

Y
Y Combinator Blog
D
Docker
有赞技术团队
有赞技术团队
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
H
Help Net Security
美团技术团队
MyScale Blog
MyScale Blog
B
Blog RSS Feed
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
G
Google Developers Blog
月光博客
月光博客
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs

博客园 - trace

flash8与javascript集成 “核弹”击中晚期直肠癌 驳图王:轻轻一招,获取上万IP SEO技巧一 很实用的缓动函数 [原创]flash动态改变注册点解决方案 flash+webservice 乱码问题解决一例(原创) 为flash构建asp.net Webservice flash 与 webservice 通信的两种方式 Flash 与 Web Service 技术的完美结 FLASH与WebService Flash 与 Web Service 技术的完美结合 Flash常用代码的介绍 flash to js flash 与 后台语言通讯 flash与js通讯(2) flash to js 使用工具包 用Javascript实现鼠标拖拽网页表单 (二) 用Javascript实现鼠标拖拽网页表单(一)
ASP.NET 2.0母版页(MasterPage)
trace · 2007-09-14 · via 博客园 - trace

使用 ASP.NET 母版页可以为应用程序中的页创建一致的布局。单个母版页可以为应用程序中的所有页(或一组页)定义所需的外观和标准行为。然后可以创建包含要显示的内容的各个内容页。当用户请求内容页时,这些内容页与母版页合并以将母版页的布局与内容页的内容组合在一起输出。

介绍
母版页(MasterPage)就相当于模板页,挺简单的,没什么好说的。基于母版页的常用的功能有:母版页和内容页之间信息的传递,在内容页中用FindControl方法找到内容页中的控件等。另外,母版页是可以嵌套的。

关键
在内容页的头部加上母版页的强类型引用

<%--创建对母版页的强类型引用,并指定到母版页的虚拟路径--%>
<%@ MasterType VirtualPath="~/MasterPage/MasterPage.master" %>

1、内容页传递数据到母版页 - 母版页创建一个公共方法,然后内容页通过“Master.方法”来调用这个公共方法

2、母版页传递数据到内容页 - 母版页创建一个公共事件来传递数据,然后内容页处理这个事件

3、内容页中用FindControl方法找到内容页中的控件 - 用“Master.FindControl("ContentPlaceHolder1").FindControl("你要查找的控件ID")”来查找

4、嵌套母版页 - 说起来麻烦,看源码吧

示例
主母板页
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>


MasterPage/MasterPage.master.cs


内容页
MasterPage/Test.aspx


MasterPage/Test.aspx.cs

[源码下载]