Webform---母版页(Master Pages)
母版页(Master Pages)为网站内的其他页面提供模版。
Master Page 使您有能力为 web 应用程序中的所有页面(或页面组)创建一致的外观和行为。
Master Page 为其他页面提供了模版,带有共享的布局和功能。Master Page 为内容定义了可被内容页面覆盖的占位符。而输出结果就是 Master Page 和内容页面的组合。
内容页包含您希望显示的内容。
当用户请求内容页时,ASP.NET 会对页面进行合并以生成输出,输出结果对 Master Page 的布局和内容页面的内容进行了合并。
=======================================================================================================
导航和页脚母页版
MP1.master:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MP1.master.cs" Inherits="MP1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="csss/css1.css" rel="stylesheet" /> <script src="<%=abc() %>"></script>//解决JS文件路径不统一的问题 <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <div> <div id="header"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> <div id="footer"></div> </div> </form> </body> </html>
MP1.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 MP1 : System.Web.UI.MasterPage { public void mp1_aaa(string s) { TextBox1.Text = s;//接收传值 } public string abc() { return ResolveClientUrl("js/js1.js"); } protected void Page_Load(object sender, EventArgs e) { } }
嵌入了mp1的左标签母页版
MP2.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 MP2 : System.Web.UI.MasterPage { public void aaa(string s) { TextBox1.Text = s; MP1 mp1 = this.Master as MP1;//传值 mp1.mp1_aaa(s); } protected void Page_Load(object sender, EventArgs e) { } }
MP2.master
<%@ Master Language="C#" MasterPageFile="~/MP1.master" AutoEventWireup="true" CodeFile="MP2.master.cs" Inherits="MP2" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> <style> #left { position: relative; width: 20%; height: 300px; background-color: yellow; float: left; } #right { position: relative; width: 80%; height: 300px; background-color: aqua; float: left; } </style> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <div id="left"> 这里是标题1<br /> <br /> 这里是标题1<br /> <br /> 这里是标题1<br /> <br /> 这里是标题1<br /> <br /> 这里是标题1<br /> <br /> 这里是标题1<br /> <br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> <div id="right"> <asp:ContentPlaceHolder ID="MP2_Content" runat="server"></asp:ContentPlaceHolder> </div> <div style="clear: both;"></div> </asp:Content>
这里是MP1掏出来的第二个页面
<%@ Page Title="" Language="C#" MasterPageFile="~/MP1.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <h1>这里是MP1掏出来的第二个页面</h1> </asp:Content>
这里是MP2套出来的页面
<%@ Page Title="" Language="C#" MasterPageFile="~/MP2.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <asp:Content ID="Content1" ContentPlaceHolderID="MP2_Content" Runat="Server"> <h1>这里是MP2逃出来的页面11111</h1> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" /> </asp:Content>
mp2套出来的页面.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 Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { //1、把本页面中的文本框的值取出来 string s = TextBox1.Text; //2、把取出来的值放到母版页的文本框中去 MP2 mp2 = this.Master as MP2; mp2.aaa(s); } }