Http://msdn.microsoft.com/zh-cn/library/wtxbf3hh.aspx
, Create template page
1) First, create a website (figure 1 ),.
2) Add a template page on this project, right-click the website, select Add new phase, find the template page (Figure 2), and set the name by yourself, of course you don't need to talk about the language. Click Add!
3) Now let's take a closer look at the functions and mysteries of this template page! We double-click MasterPage. master File, view its original file, we first see the first line is "<% @ Master Language =" C # "AutoEventWireup =" true "CodeFile =" MasterPage. master. cs "Inherits =" MasterPage "%>", which is the declaration of the template page. If we look at it carefully, we will find that its declaration is similar to that of the general page, the main points are as follows: 1. The template page starts with the master, and the suffix is ". master, and the general page starts with page, with the suffix ". aspx ", another major difference is that there is a" ContentPlaceHolder "Control in the template page. This control is used to fill the content page, but not the common page! (Figure 4) We can add various fixed information such as the logo or declaration on the template page! Now let's add a content page for this template page to see the effect.
The procedure is as follows: Right-click the content page and select "add content page" (figure 6. At this point, we will see the source code area of the content page, and its simplicity, just three sentences.
"MasterPageFile = "~ /MasterPage. master "is defined as a template page,
Next line: "<asp: Content ID =" Content1 "ContentPlaceHolderID =" ContentPlaceHolder1 "Runat =" Server ">
</Asp: Content> ",
"ContentPlaceHolderID =" ContentPlaceHolder1 "" specifies the ContentPlaceHolder that can be added to the content page on the template page. Sometimes our template page does not have more than one ContentPlaceHolder, we need to choose based on our own needs! Let's Select the "design" mode to see the effect. We can only edit the content page, which realizes the role of the template page! If our content page also wants to use the content on the template page, we can click the triangle in the upper right corner of ContentPlaceHolder and select "Default as the template page content" (figure 8 )!
On the master page, you can add multiple ContentPlaceHolder controls and drag them to the master page by selecting ContentPlaceHolder in the standard bar of the Toolbox. In the ContentPlaceHolder area, you can directly enter the content or add an ASP. NET control. The content in the ContentPlaceHolder area is displayed as the default content of the Content Page.
2. Use the template page
1), select the website name, right-click, select the "Add new project" command, and select "WEB form" in the template list ". There are two check boxes in the lower right corner of the add new project form. Make sure to select the "select master page" check box.
2). In the pop-up window, select the master and select the desired master.
3) after saving, go to the new web page, that is, the content page. When displayed, the Content of the master page is displayed in the form of watermark fading. The ContentPlaceHolder control area on the master page is replaced by the Content control on the Content Page,
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. 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.
(CHE Yanlu)
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