Asp. NET in the master page role
One is to improve the reuse of code (the same code is extracted)
The second is to keep the whole site consistent style and style.
The existence of a master page must be the existence of a content page, or the existence of a master page has no meaning.
. master
One, add master page
In the new master page you will find that two ContentPlaceHolder controls are automatically generated
One in the head area, id= "Head";
One in the body area, the default id= "ContentPlaceHolder1"
--This is two placeholder controls, where the text position is reserved.
Level Two master Page
Master pages can be nested, that is, the master page is built on top of the original master page.
Nested master pages do not automatically generate ContentPlaceHolder controls and need to be written manually
first-level master page
<%@ Master language="C #"masterpagefile="~/mp1.master"autoeventwireup="true"codefile="MP2.master.cs"inherits="MP2"%><asp:content id="Content1"Contentplaceholderid="Head"runat="Server"></asp:Content> <%--First-level master page reserved Pits--%><asp:content id="Content2"Contentplaceholderid="ContentPlaceHolder1"runat="Server"> <%--First-level master page reserved pits--%> <div style="width:30%; float:left; height:500px; Background-color:green ;">This is a menu option<br/> <br/>This is a menu option<br/> <br/>This is a menu option<br/> <br/> <asp:textbox id="TextBox1"runat="Server"></asp:TextBox> </div> <div style="width:70%; float:left; height:500px; Background-color:yellow ;"> <asp:contentplaceholder id="Mp2_content1"runat="Server"> </asp:ContentPlaceHolder> <%--Two-level formwork dug pits, left behind to fill--%> </div> < Div style="Clear:both;"></div> <asp:contentplaceholder id="Mp2_js"runat="Server"></asp:ContentPlaceHolder></asp:Content>
View Code
Iii. building a Web form that uses a master page
When you create a new Web form, select the master page you want to apply
1, apply the first-level template page
There are two content controls in the new Web form that correspond to the first-level master page
<%@ 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"> View Code2. Apply level two master page
There is only one content control in the new Web form, and the one you dug
<%@ Page title=""Language="C #"masterpagefile="~/mp2.master"autoeventwireup="true"codefile="Default3.aspx.cs"inherits="DEFAULT3"%><asp:content id="Content1"Contentplaceholderid="Mp2_content1"runat="Server"> View CodeIv. transfer of data between master pages and sub-pages :
There is no way to access the inside of a master page from outside in a child page
Can write public methods to pass values in the form of a calling method
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classdefault3:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; } //Click events voidButton1_Click (Objectsender, EventArgs e) { strings =textbox1.text;//get the value of a text boxLabel1.Text=s; MP2 m2= This. Master asMP2;//Gets the master page of the current pageM2.AAA (s);//method to invoke this master page }}
Child pageusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classmp2:system.web.ui.masterpage{protected voidPage_Load (Objectsender, EventArgs e) { } Public voidaaastringA//public methods in a level two master{TextBox1.Text= A;//assign the passed-in value to the TextBox1MP1 M1= This. Master asMP1;//Gets the master page of the current pageM1.M1_AAA (a);//methods to invoke the master page of this page }}
Level Two master pageusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classmp1:system.web.ui.masterpage{protected voidPage_Load (Objectsender, EventArgs e) { } Public voidM1_AAA (stringa) {TextBox1.Text= A;//Method}
first-level master pageSub-page--level two master--one level master layer pass
Master Page The common external style sheet path matches the path of the external JS file:
Webfrom "master page"