How to Use the Asp.net master page

Source: Internet
Author: User
Tags blank page

The master page is a new concept introduced in vs2005. It achieves the modularization of the interface design and achieves code reuse. It is like a wedding dress template in a wedding dress photo studio. the same wedding dress template can be used by different newcomers. as long as their photos are pasted on the existing wedding dress template, a beautiful wedding dress photo can be formed, this greatly simplifies the design complexity of wedding art photos. The master page is like a wedding dress template, and the content page is like a photo of two newcomers.
There is no master page in vs2003. To achieve this design reuse effect, we can only use the "user control", but the user control does not have a visual combination appearance, it is not easy to use.

Master page (the extension is. Master)
Similar to common pages, it can be visually designed or written with post code. Unlike a normal page, it can contain the contentplaceholder control, which is the area where the content page can be displayed.
The Code is as follows:
<% @ 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. The Declaration indicator here is "<% @ master... %>"
2. It contains the <asp: contentplaceholder...> control.

Content Page (the extension is. aspx)
When creating the content page, select the "select master page" check box in the "Add new project" dialog box. The created page is the content page. When the content page is displayed, the content of the master page is displayed as a watermark, the contentplaceholder control area on the master page will be replaced by the content control on the content page, where programmers can write content on 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. A masterpagefile = "~ /Masterpage/MP. Master ", which is generated based on the selection of the" select master page "check box when creating the content page. It specifies the content page and the master page of the Content Page.
2. "<asp: content...>" is the content to be displayed.

1. Write background code on the master page to access the controls on the master page:
Like the normal ASPX page, double-click the button to write the code on the master page.

2. Write background code on the inner blank page to access the controls on the Content Page:
Like the normal ASPX page, double-click the button to write the code on the master page.

3. write code on the Content Page to access the controls on the master page:
There is a master object in the content page. It is of the masterpage type and represents the master page of the current content page. Through the findcontrol method of this object, we can find the control in the parent layout, so that we can operate the control in the master page on the Content Page.
Textbox TXT = (textbox) (masterpage) Master). findcontrol ("txtmaster ");
TXT. Text = this.txt content1.text;

 

 4. Write the code on the Content Page to access the attributes and methods on the master page:
It is still possible to access through the master object, but here we need to convert the master object to a specific master page type, and then call the attributes and the square in the master page.
It should be noted that:The attributes and methods to be called on the content page on the master page must be modified in public mode. Otherwise, it cannot be adjusted.
Assume that the master page has the following attributes and methods:
Public String textvalue
{
Get
{
Return this.txt master. text;
}
Set
{
This.txt master. Text = value;
}
}
Public void show (string Str)
{
Txtmaster. Text = STR;
}
On the content page, you can use the code of the next generation to call methods on the master page:
(Masterpage_mp1_master).show(this.txt content1.text );
(Masterpage_mp) Master). textvalue = this.txt content1.text;

 

5. Access the content page controls on the master page:
On the master page, you can call the findcontrol method in the contentplaceholder control to obtain the control and then operate the control.
(Textbox) This. contentplaceholder1.findcontrol ("txtcontent1"). Text = this.txt master. text;

6. Access methods and attributes on the content page on the master page:
It is difficult to call the attributes and methods in the subpage on the master page, because we cannot find the methods and properties through findcontrol as in the previous step.
So we want to add the following code to the Declaration indicator of the master plane:
<% @ Reference page = "~ /Masterpage/show1.aspx "%>
An error is returned during running. The error message is "circular reference cannot be implemented ". This is because the master page is referenced in the Child page by default, and you cannot reference the child page in the master page.
I did not find a better solution on the Internet, but it reminds us that C # is a "Reflection", which allows us to Dynamically Retrieve page objects, you can also call its attributes and methods.
The Code is as follows:
Type T = This. contentplaceholder1.page. GetType ();
Propertyinfo Pi = T. getproperty ("contentvalue"); // get the contentvalue attribute
Pi.setvalue(this.contentplaceholder1.page,this.txt master. Text, null); // assign a value to the attribute

Methodinfo MI = T. getmethod ("setvalue"); // gets the setvalue () method
Object [] OS = new object [1]; // build input parameters
OS [0] = txtmaster. text;
Mi. Invoke (this. contentplaceholder1.page, OS); // call the setvalue Method

7. Perform different operations on different content pages on the master page when multiple content pages are used.
You can add multiple different content pages to the master page, but during the design period, we cannot know which content page is currently running. Therefore, you can only use the branch to determine which sub-page is currently running and perform different operations. Reflection knowledge is also used here.
The Code is as follows:
String S = This. contentplaceholder1.page. GetType (). tostring (); // type name of the retrieved Content Page
If (S = "ASP. default17_aspx") // perform different operations based on 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. JS Code operations on the parent layout and content page
An ID is automatically generated after the control on the master page or content page is run. For example, if the ID of the text box is txtcontent1, the ID is automatically changed to ctl00_contentplaceholder2_txtcontent1 after the control is run, the name attribute changes to ctl00 $ contentplaceholder2 $ txtcontent1.
In js code, we use the document. getelementbyid () method. When obtaining the control object based on the ID, we should use the ID ctl00_contentplaceholder2_txtcontent1. Otherwise, an "unfound object" exception occurs.

 

Master page Running Mechanism
A master page is only a page template. A single master page cannot be accessed by users. Separate content pages cannot be used. The master page and content page have a strict correspondence relationship. The number of contentplaceholder controls contained in the master page. Therefore, you must set the corresponding content control on the Content Page. When the client browser sends a request to the server asking to browse a content page, the ASP. net engine executes the code of the Content Page and master page at the same time and sends the final result to the client browser.
The master page and content page can be summarized into the following five steps.
(1) The user requests a page by entering the URL of the Content Page.
(2) read the @ page command after obtaining the content page. If the command references a master page, the master page is also read. If the two pages are requested for the first time, both pages must be compiled.
(3) Merge the master page to the Control tree of the content page.
(4) The content of each content control is merged into the corresponding contentplaceholder control on the master page.
(5) display the result page.

 

Master page and content page Event Sequence

(1) control init event on the master page;
(2) content control init event on the content page;
(3) Init event on the master page;
(4) Content Page init event;
(5) Content Page load event;
(6) master page load event;
(7) content control load event on the content page;
(8) Content Page prerender event;
(9) prerender event on the master page;
(10) The prerender event of the master page control.
(11) content control prerender event on the Content Page.

Advantages of using a master page:
(1) It is conducive to site modification and maintenance and reduces the work intensity of developers.
(2) Facilitate page layout
(3) provide an easy-to-use Object Model

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.