Talking about the related knowledge of Asp. Net master page
- 2021-11-01 02:50:22
- OfStack
Knowledge of Asp. Net Master Pages
Master pages are used like regular pages, where you can place files or graphics, any HTML and Web controls, post-code, and so on. The extension of the master page ends in. master and cannot be viewed directly by the browser. Master pages must be used by other pages before they can be displayed.
It is used with ordinary page 1 like, can be visual design, can also write post-code. Unlike a regular page, it can contain an ContentPlaceHolder control, and an ContentPlaceHolder control is an area where content pages can be displayed.
A master page is only a page template, and a separate master page cannot be accessed by users. Separate content pages cannot be used either. Master pages and content pages have a strict correspondence. How many ContentPlaceHolder controls are contained in the master page, then the corresponding Content control must also be set in the content page. When the client browser makes a request to the server to browse a content page, the engine executes the code for both the content page and the master page, and sends the final result to the client browser.
Master pages have the following advantages:
Master pages allow you to centralize the common functionality of a page so that updates can be made in only 1 location. Master pages make it easy to create 1 set of controls and code and apply the results to 1 set of pages. For example, you can use controls on a master page to create a menu that applies to all pages. Master pages allow you to control the layout of the final page in detail by allowing you to control how placeholder controls are rendered. Master pages provide an object model that allows you to customize master pages from individual content pages.Sample code:
<%@ Master Language= " C# " AutoEventWireup= " true " CodeFile= " MasterPage.master.cs " Inherits= " MasterPage " %>
...
<form id= " form1 " runat= " server " >
<div>
<asp:contentplaceholder id= " ContentPlaceHolder1 " runat= " server " >
</asp:contentplaceholder>
</div>
</form>
...
Note:
1, where the declaration indicator is " < % @ Master...% > "
2. Its internal contents < asp: contentplaceholder … … > Control
Content Page (Extension. aspx)
When creating a content page, select the Select Master Page check box in the Add New Item dialog box. The page established in this way is the content page. When the content page is displayed, the content of the master page will be displayed in the form of watermark dilution, and the ContentPlaceHolder control area in the master page will be replaced by Content control in the content page, where programmers can write the content of the content page.
The code is as follows:
<%@ Page Language= " C# " MasterPageFile= " ~/MasterPage/MP.master " AutoEventWireup= " true " CodeFile= " Show1.aspx.cs " Inherits= " MasterPage_Show1 " Title= " Untitled Page " %>
<asp:Content ID= " Content1 " ContentPlaceHolderID= " ContentPlaceHolder1 " Runat= " Server " >
</asp:Content>
Note:
1. The declaration indicator here has an additional entry MasterPageFile= "~/MasterPage/MP. master", which is generated when the content page is created based on the selection of the "Select Master Page" check box. It indicates that the page is the content page and which page is the master page of the content page.
2. " < asp: Content … … > "Is what you want to display in it.
1. Write background code in the master page to access the controls in the master page:
Just like a normal aspx page 1, double-click the button to write the code in the master page
2. Write background code in the inner page to access the controls in the content page:
Just like the normal aspx page 1, double-click the button to write the code in the master page
3. Write code in the content page to access the controls in the master page:
There is an Master object in the content page, which is of type MasterPage and represents the master page of the current content page. Through the FindControl method of this object, we can find the controls in the master page, so that we can manipulate the controls in the master page in the content page.
TextBox txt = (TextBox)((MasterPage)Master).FindControl( " txtMaster " );
txt.Text = this.txtContent1.Text; ;
4. Write code in the content page to access the properties and methods in the master page:
It is still possible to access through an Master object, except that it is illegal to convert the Master object to a specific master page type and then call the properties and parties in the master page.
Here, the properties and methods in the master page to be invoked by the content page must be modified by Public. Otherwise, you can't be transferred to.
Suppose you have the following properties and methods in your master page:
public string TextValue
{
get
{
return this.txtMaster.Text;
}
set
{
this.txtMaster.Text = value;
}
}
public void show(string str)
{
txtMaster.Text = str;
}
In the content page, you can call the method in the master page through the next generation of code:
((MasterPage_MP)Master).show(this.txtContent1.Text);
((MasterPage_MP)Master).TextValue = this.txtContent1.Text;
5. Controls that access the content page in the master page:
In the master page, you can get the control by calling the FindControl method in the ContentPlaceHolder control, and then operate on the control.
((TextBox)this.ContentPlaceHolder1.FindControl( " txtContent1 " )).Text = this.txtMaster.Text;
6. Access the methods and properties on the Content page in a master page:
Calling the properties and methods in the child pages in the master page is a little difficult, because we can't find the methods and properties through FindControl as we did in the previous step 1.
So we thought of adding the following code to the declaration indicator of the master layout:
<%@ Reference Page= " ~/MasterPage/Show1.aspx " %>
When running, an error was found, and the content of the error was "circular reference cannot be implemented". This is because the master page is referenced in the child page by default, so you can no longer reference the child page in the master page.
I haven't found a better solution online either, but it reminds us of the "reflection" that C # is, which allows us to dynamically fetch a page object and call its properties and methods.
The code is as follows:
Type t = this.ContentPlaceHolder1.Page.GetType();
PropertyInfo pi = t.GetProperty( " ContentValue " ); // Get ContentValue Attribute
pi.SetValue(this.ContentPlaceHolder1.Page,this.txtMaster.Text,null); // Assign a value to an attribute
MethodInfo mi = t.GetMethod( " SetValue " ); // Get SetValue() Method
object[] os = new object[1]; // Build input parameters
os[0] = txtMaster.Text;
mi.Invoke(this.ContentPlaceHolder1.Page, os); // Call SetValue Method
7. In the case of multiple content pages using the master page, implement different operations according to different content pages in the master page
Multiple different content pages can be added to the master page, but during design time we don't know which content page is currently running. Therefore, you can only judge which sub-page is currently running by branches to perform different operations. The knowledge of reflection is also used here.
The code is as follows:
string s = this.ContentPlaceHolder1.Page.GetType().ToString(); // Fetch the type name of the content page
if (s == " ASP.default17_aspx " ) // Perform different actions according to different content page types
{
((TextBox)this.ContentPlaceHolder1.FindControl( " TextBox2 " )).Text = " MastPage " ;
}
else if (s == " ASP.default18_aspx " )
{
((TextBox)this.ContentPlaceHolder1.FindControl( " TextBox2 " )).Text = " Hello MastPage " ;
}
8. Operation of the JS code in the Master and Content pages
ID is automatically generated after a control in a master page or content page is run. For example, ID of a text box is txtContent1, ID is automatically changed to ctl00_ContentPlaceHolder2_txtContent1 after running, and the name property is changed to ctl00 $ContentPlaceHolder2 $txtContent1.
In the JS code, we use the document. getElementById () method. When we get the control object according to id, we should use the ID name ctl00_ContentPlaceHolder2_txtContent1, otherwise an exception of "No object found" will be generated.
Master page operation mechanism
A master page is only a page template, and a separate master page cannot be accessed by users. Separate content pages cannot be used either. Master pages and content pages have a strict correspondence. How many ContentPlaceHolder controls are contained in the master page, then the corresponding Content control must also be set in the content page. When the client browser makes a request to the server to browse a content page, the ASP. NET engine executes the code for both the content page and the master page and sends the final result to the client browser.
The running process of master page and content page can be summarized as the following five steps.
(1) The user requests a page by typing the URL of the content page.
(2) After getting the content page, read the @ Page directive. If the directive references 1 master page, the master page is also read. If these two pages are requested for the first time, both pages will be compiled.
(3) The master page is merged into the control tree of the content page.
(4) The contents of each Content control are merged into the corresponding ContentPlaceHolder control on the master page.
(5) Render the resulting page.
Master Page and Content Page Event Sequence
(1) Control Init event in master page;
(2) Init event of Content control in content page;
(3) Master page Init event;
(4) Content page Init event;
(5) Content page Load event;
(6) Master page Load event;
(7) Load event of Content control in content page;
(8) Content page PreRender event;
(9) Master page PreRender event;
(10) Master Page Control PreRender Event.
(11) The Content control PreRender event in the content page.