`

在母版页和子页面之间传递数据

阅读更多
在母版页和子页面之间传递数据

引入数据

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

<!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>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>


MyMasterPage.master.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyMasterPage : System.Web.UI.MasterPage
{
    public string MyValue
    {
        get { return "My Value in Master Page"; }
        set { }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}


子页面
Default.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MyMasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="myLabel" runat="server" Text=""></asp:Label>
</asp:Content>


Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MyMasterPage my=(MyMasterPage)Page.Master;
        myLabel.Text = my.MyValue;
    }
}


Sample Two
动态修改Master内容
MyMasterPage.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MyMasterPage.master.cs"
    Inherits="MyMasterPage" %>

<!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>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>


MyMasterPage.master.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyMasterPage : System.Web.UI.MasterPage
{
    private string myValue;
    public string MyValue
    {
        get { return myValue; }
        set { myValue = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        myValue = "You done in Master";
    }
}


Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        MyMasterPage mmp = (MyMasterPage)Page.Master;
        myLabel.Text = mmp.MyValue;
    }
}


2011-6-1 15:16 danny

分享到:
评论

相关推荐

    ASP.NET项目实训:“新闻发布系统”首页设计--页面设计.ppt

    知识准备 母版的使用 母版页的作用类似于DreamWeaver中的模板,扩展名是.master,母版页包含页面中所有固定不变的内容,母版页中的内容将显示在所有的页面中。由于这些元素的统一布局,保证了整个程序中所有页面...

    ASP.NET复习提纲有答案版

    1. Xml在.NET中使用有利于跨平台传输数据。 2. 母版页Common存放位置,他的优点?P83应用程序中的根目录, 1. 有利于站点维护和修改,降低开发人员的工作强度 2. 提供高效的内容整合能力 3. 有利于实现页面布局 4...

    个人简历网站(ASP)

    利用ASP所学的知识做一个个人简历网站,里面包含将数据存入数据库中,并导出在xml文档中,还利用了母版页,Timer控件,数据绑定,页面切换与数据传递等技术

    ASP.NET3.5从入门到精通

    第 12 章 ASP.NET 的皮肤、主题和母版页 12.1 皮肤和主题 12.1.1 CSS 简介 12.1.2 CSS 基础 12.1.3 CSS 常用属性 12.1.4 将CSS 应用在控件上 12.1.5 主题和皮肤 12.1.6 页面主题和全局主题 12.1.7 应用和禁用主题 ...

    ASP.NET 控件的使用

    5.1.3 在母版页中使用图片和超链接 170 5.1.4 在Web配置文件中注册母版页 172 5.2 修改母版页内容 172 5.2.1 使用Title属性 173 5.2.2 使用Page Header属性 173 5.2.3 暴露母版页属性 175 5.2.4 对母版页使用...

    ASP.NET 3.5 开发大全word课件

    第12章 ASP.NET的皮肤、主题和母版页 12.1 皮肤和主题 12.1.1 CSS简介 12.1.2 CSS基础 12.1.3 CSS常用属性 12.1.4 将CSS应用在控件上 12.1.5 主题和皮肤 12.1.6 页面主题和全局主题 12.1.7 应用和禁用主题 12.1.8 用...

    ASP.NET.4揭秘

    5.1.3 在母版页中使用图片和超链接191 5.1.4 在web配置文件中注册母版页193 5.2 修改母版页内容194 5.2.1 使用title属性194 5.2.2 使用page.header属性194 5.2.3 暴露母版页属性196 5.2.4 对母版页使用findcontrol...

    ASPNET35开发大全第一章

    第12章 ASP.NET的皮肤、主题和母版页 12.1 皮肤和主题 12.1.1 CSS简介 12.1.2 CSS基础 12.1.3 CSS常用属性 12.1.4 将CSS应用在控件上 12.1.5 主题和皮肤 12.1.6 页面主题和全局主题 12.1.7 应用和禁用主题 12.1.8 用...

    ASP.NET 3.5 开发大全11-15

    第12章 ASP.NET的皮肤、主题和母版页 12.1 皮肤和主题 12.1.1 CSS简介 12.1.2 CSS基础 12.1.3 CSS常用属性 12.1.4 将CSS应用在控件上 12.1.5 主题和皮肤 12.1.6 页面主题和全局主题 12.1.7 应用和禁用主题 12.1.8 用...

    ASP.NET 3.5 开发大全

    第12章 ASP.NET的皮肤、主题和母版页 12.1 皮肤和主题 12.1.1 CSS简介 12.1.2 CSS基础 12.1.3 CSS常用属性 12.1.4 将CSS应用在控件上 12.1.5 主题和皮肤 12.1.6 页面主题和全局主题 12.1.7 应用和禁用主题 12.1.8 用...

    ASP.NET 3.5 开发大全1-5

    第12章 ASP.NET的皮肤、主题和母版页 12.1 皮肤和主题 12.1.1 CSS简介 12.1.2 CSS基础 12.1.3 CSS常用属性 12.1.4 将CSS应用在控件上 12.1.5 主题和皮肤 12.1.6 页面主题和全局主题 12.1.7 应用和禁用主题 12.1.8 用...

    ASP.NET4高级程序设计第4版 带目录PDF 分卷压缩包 part1

    16.4.4 具有表格和CSS布局的母版页 16.4.5 母版页和相对路径 16.4.6 通过配置文件应用母版页 16.5 高级母版页 16.5.1 和母版页类交互 16.5.2 动态设置母版页 16.5.3 嵌套母版页 16.6 总结 第17章 ...

    ASP.NET教学讲义,完整章节

    6.3 创建母版页 109 6.4 创建内容页 111 6.5 皮肤和主题的概念 113 6.6 样式的应用 114 6.7 主题的构成与构建 115 6.8 设置站点级别的样式 116 6.9 站点导航 117 6.10 导航控件 118 第七章:ASP.NET的安全性 120 7.3...

    asp.net教学讲义

    6.2.1 母版页基础知识 96 6.2.2母版页运行机制 98 6.2.3 母版页的优点 100 6.3 创建母版页 100 6.4 创建内容页 102 6.5 皮肤和主题的概念 104 6.6 样式的应用 105 6.6.1 对单独页面元素使用样式 105 6.6.2 将样式...

    ASP.NET4高级程序设计(第4版) 3/3

    内容简介  《ASP.NET 4高级程序设计(第4版)》是ASP.NET领域的鸿篇巨制,全面讲解了ASP.NET...16.4.4 具有表格和CSS布局的母版页 556 16.4.5 母版页和相对路径 559 16.4.6 通过配置文件应用母版页 559 16.5 高级...

    零基础学ASP.NET 2.0电子书&源代码绝对完整版1

    MasterPage.master 一个简单的母版页。 9-01.aspx 引用母版页。 MasterPage1.master 创建一个母版页。 9-01.aspx 调用母版页并进行交互。 FMasterPage.master 进行嵌套的父母版页。 SMasterPage....

    《计算机应用基础(本科)》21春电子科技大在线作业2.docx

    备注母版的页面共有5个设置:页眉区、页脚区、日期区、幻灯片缩图和数字区 D.备注母版的下方是备注文本区,可以像在幻灯片母版中那样设置其格式 答:———— 11. 数字音频采样和量化过程所用的主要硬件是()。 A.数字...

    ASP.NET编程之道.part1.rar

    陷阱26 使用查询字符串在页面间传递参数 陷阱27 通用数据类型运算产生的陷阱 陷阱28 在模态窗口中下载文件 陷阱29 构造方法中调用虚方法的陷阱 陷阱30 使用值类型进行线程同步 第4章 开发人员意识中的20个常见谬误 ...

    东大22春《计算机应用基础》在线平常作业2.docx

    A、"画图"的辅助工具 B、存储图形或数据的物理空间 C、"写字板"的重要工具 D、各种应用程序之间数据共享和交换的工具 17.防火墙软件一般用在____。 A、工作站与工作站之间 B、服务器与服务器之间 C、工作站与服务器...

Global site tag (gtag.js) - Google Analytics