ASP. NET master Page use--Go

Source: Internet
Author: User
Tags access properties

Http://www.cnblogs.com/_zjl/archive/2011/06/12/2078992.html

Master page is a new concept introduced in VS2005, it is very good to realize the modularization of interface design, and realize the reuse of code. It is like wedding photo studio in the wedding template, the same wedding template can be used for different couples, as long as their photos affixed to the existing wedding template can form a beautiful wedding photos, so that can greatly simplify the design complexity of wedding art photo. The master page here is like a wedding dress template, and the content page is like a photo of two new people.
There is no master page in VS2003, we can only implement this design reuse by "user control", but the user control does not have a visual combined appearance, which is inconvenient to use.

Master page (extension is. master)
It is used as a normal page, you can visualize the design, you can also write the post code. Unlike a normal page, it can contain ContentPlaceHolder controls, which are areas where content pages 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>
......

Picture path: http://hiphotos.baidu.com/mengling99/pic/item/12f2fc3d9ed40aeb9e3d6227.jpg

Attention:
1. There is one more masterpagefile= "~/masterpage/mp.master" in the declaration indicator, which is generated when the content page is created according to the selection of the Select master Page check box. It indicates that the page is a content page and also indicates which page the master page of the content page is.
2, "<asp:content ......>" is the content to be displayed therein.

First, write the background code in the master page and access the controls in the master page:
As with normal ASPX pages, you can write code in a master page by double-clicking the button

second, the inside empty page to write the background code, access the content page of the control:
As with normal ASPX pages, you can write code in a master page by double-clicking the button

Third, write code in the content page to access the controls in the master page:
In the content page, there is aMasterObject, which is the masterpage type that represents the master page of the current content page. Through this object'sFindControlmethod, we can find the controls in the master layout so that the controls in the master page can be manipulated in the content page.
TextBox txt = (textbox) ((MasterPage) Master). FindControl ("Txtmaster");
Txt. Text = This.txtContent1.Text;;

Iv. Write code in the content page to access properties and methods in the master page:
It is still possible to access it through the Master object, except to convert the Master object to a specific master page type, and then invoke the properties and the side illegality in the master page.
Here is the explanation:The properties and methods in the master page to be called by the content page must be public modified. Otherwise it cannot be transferred to.
Assume that the following properties and methods are in the 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 use the next generation of code to implement a call to a method in a master page:
((MASTERPAGE_MP) Master).Show (This.txtContent1.Text);
((MASTERPAGE_MP) Master). TextValue= This.txtContent1.Text;

v. To access the content page's controls in the master page:
In a master page, you can use theContentPlaceHolderControl is called in theFindControlmethod to get the control and then manipulate the control.
(TextBox) this. Contentplaceholder1.findcontrol ("TxtContent1")). Text = This.txtMaster.Text;

Vi. accessing methods and properties in a content page in a master page:
Calling properties and methods in a child page in a master page is a bit difficult because we can't pass it as we did in the previous stepFindControlTo find methods and properties.
So we thought of adding the following code to the declaration designator in the parent layout:
<%@ Reference page= "~/masterpage/show1.aspx"%>
The error is found when running, and the error is "unable to implement circular reference". This is because the master page is referenced by default in the child page, and you can no longer refer to the child page in the master page.
I didn't find a better solution on the Internet, but it reminded us of the "reflection" of C #, which allows us to dynamically get the Page object and can invoke its properties and methods.
The code is as follows:
Type T = this. ContentPlaceHolder1.Page.GetType ();
PropertyInfo pi = t.getproperty ("Contentvalue"); Get Contentvalue Property
Pi. SetValue (this.    Contentplaceholder1.page,this.txtmaster.text,null); Assigning a value to a property

MethodInfo mi = t.getmethod ("SetValue"); Get the SetValue () method
object[] os = new OBJECT[1]; Building Input parameters
Os[0] = Txtmaster.text;
Oil Invoke (this.    Contentplaceholder1.page, OS); Calling the SetValue method

Seven, in the case of multiple content pages using the master layout, in the master page according to different content pages to achieve different actions
There are several different content pages that can be added to a master page, but during design time, we don't know which content page is currently running. Therefore, you can only use the branch to determine which sub-page is currently running to perform different operations. The knowledge of reflection is also used here.
The code is as follows:
string s = this. ContentPlaceHolder1.Page.GetType ().   ToString (); Remove the type name of the content page
if (s = = "asp.default17_aspx")//perform different actions 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";
}

viii. operation of JS code in parent layout and content page
An ID is automatically generated after a control in a master page or content page is run, such as the ID of a text boxTxtContent1, the ID automatically changes after it is runCtl00_contentplaceholder2_txtcontent1, the Name property becomes Ctl00$contentplaceholder2$txtcontent1.

In the JS code, we use the document.getElementById () method, according to the ID of the control object, should use ctl00_contentplaceholder2_txtcontent1 this ID name, otherwise it will produce "object not found "Exception.
(Che Yanlu)
master page run mechanism
A master page is simply a page template, and a separate master page cannot be accessed by the user. A separate content page is not available. Master pages and content pages have strict correspondence. How many ContentPlaceHolder controls are included in the master page, the content page must also be set up with its corresponding contents control. When the client browser makes a request to the server, the ASP is asked to browse for a content page. NET engine executes the code for both the content page and the master page and sends the final result to the client browser.

The process of running master pages and content pages can be summarized in the following 5 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 a master page, the master page is also read. If you are requesting these two pages for the first time, two pages will be compiled.
(3) Master pages are merged into the control tree of the content page.
(4) The contents of each content control are merged into the corresponding ContentPlaceHolder control in the master page.
(5) Renders the resulting page.

master page and Content page event order

Picture path: http://hiphotos.baidu.com/mengling99/pic/item/a4b8984bcd0d42dc83025c21.jpg

(1) Control init event in master page;
(2) Content control init event in the Contents page;
(3) master page init event;
(4) content page init event;
(5) content page Load event;
(6) master page load event;
(7) Content control Load event in the contents page;
(8) content page PreRender event;
(9) master page PreRender event;
(10) master page Control PreRender event.
(11) Content control PreRender event in the Contents page.

Advantages of using master Pages:
(1) Facilitate site modification and maintenance, reduce the intensity of the work of developers
(2) facilitates page layout
(3) provide an easy-to-use object model

ASP. NET master Page use--Go

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.