母版的建立 传值 文件路径
1、一个单独的母版
(1)新建母版
(2)设置样式和内容
(3)新建窗体,引用模板。
2、在模板页中设置新的母版
右键--添加--添加新项--模板页(设置文件名)--选中所要继承的母版--确定
注意:在“次一级”的模板中,需要手动添加模板中的“坑“,以备以后引用母版进行编写,如下图
3、母版的传值
第一(一个母版间的传值)在Mp1中添加textBox,在引用页添加textBox和button,点击按钮,使引用页的值传到母版的textBox中
(1)建立母版页MP1
<%@ 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="css/cssa.css" rel="stylesheet" /> <script src="<%=yingshe("js/JavaScript.js") %>">"></script> <asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder> <style> * { margin:0px; padding:0px; } #top { width:100%; height:100px; background-color:yellow; } #bottom { width:100%; height:100px; background-color:green; } </style> </head> <body> <form id="form1" runat="server"> <div> <div id="top"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> <div id="bottom"></div> </div> </form> </body> </html>
(2)引用MP1的页面(Default.aspx)
<%@ Page Title="" Language="C#" MasterPageFile="~/MP1.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:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </asp:Content>
(3)Default.aspx的代码操作区(重点)
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 Button1_Click(object sender, EventArgs e) { string a = TextBox1.Text;//把所填内容取出来 MP1 p1= this.Master as MP1;//把该按钮的母版页取出来 TextBox t = p1.FindControl("TextBox1") as TextBox;//找到母版页中要赋值的TextBox t.Text = a;//把所填内容给母版页的TextBox } }
第二(两个母版间的传值)
(1)建立母版MP1和MP2
MP1
<%@ 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="css/cssa.css" rel="stylesheet" /> <script src="<%=yingshe("js/JavaScript.js") %>">"></script> <asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder> <style> * { margin:0px; padding:0px; } #top { width:100%; height:100px; background-color:yellow; } #bottom { width:100%; height:100px; background-color:green; } </style> </head> <body> <form id="form1" runat="server"> <div> <div id="top"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><%--//传值区--%> </div> <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> <div id="bottom"></div> </div> </form> </body> </html>
MP1的一个传值方法
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 string yingshe(string a) { return ResolveClientUrl(a); } public void shuan(string a)//新建方法,把a值传给MP1的TextBox1 { TextBox1.Text = a; } protected void Page_Load(object sender, EventArgs e) { } }
MP2
<%@ Master Language="C#" MasterPageFile="~/MP1.master" AutoEventWireup="true" CodeFile="MP2.master.cs" Inherits="MP2" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <style> #a { width:20%; background-color:aqua; float:left; } #b{ width:80%; height:100px; background-color:gray; float:left; } </style> <div id="a"> 中心<br /> 事业<br /> 爱情<br /> 逗逼<br /> 土匪<br /> 身份<br /> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br /> </div> <div id="b"> <asp:ContentPlaceHolder ID="MP2_ContentPlaceHolder" runat="server"> </asp:ContentPlaceHolder> </div> </asp:Content>
MP2的一个传值方法
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 chuan(string a)//新建的一个方法chuan,把a的值传给MP2的TextBox2,并且把a值传给MP1 { TextBox2.Text = a; MP1 p1 = this.Master as MP1; p1.shuan(a);//MP1的一个方法 } protected void Page_Load(object sender, EventArgs e) { } }
(2)建立引用MP2的页面Default.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MP2.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <asp:Content ID="Content1" ContentPlaceHolderID="MP2_ContentPlaceHolder" Runat="Server"> <style> #aa { background-color:orange; } </style> <div id="aa">这是母版2的D3</div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </asp:Content>
(3)Default.aspx的代码页面
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) { } protected void Button1_Click(object sender, EventArgs e) { string a = TextBox1.Text; MP2 p2 = this.Master as MP2; p2.chuan(a);//chuan()为MP2的一个方法 } }
(4)文件路径
在模板中,对于CSS文件,无论文件位置变或者不变,在文件中CSS文件引用位置,系统会自动相应地改变。
但是JS文件就不会,那么就需要模板页和母版的代码区中有相应地操作
模板页
母版代码区
完!!!