A master page is a special file referenced by applications (and pages) and contains the static layout of pages. Each sub-page can customize a region and reference a special placeholder control on the master page. Child pages are the combination of code blocks. The Runtime library uses them to fill the master page. The content of the master page is merged into the content page. The two are combined to dynamically generate a new page class and respond to user requests. The merge process occurs only once during compilation.
Processing of master pages and content pages
The use of the master page has slightly changed the page processing and compilation methods. First, the page based on the master page has two dependencies: the aspx source file of the content page and the master file of the master page. If either of the two pages is changed, the dynamic page assembly will be rebuilt. Although the URL required by the user is a content page, the final page sent to the browser also needs to use the master page, and the content page provides the corresponding replacement content.
Compile the master page
ASP. the net Runtime Library tracks the dependencies between the aspx and the master page. This information will be stored in ASP.. net. Then, the Runtime Library parses the source code of the master page and creates a VB or C # class based on the language set on the master page. This class inherits from the code file of masterpage or master page and is then compiled into an assembly. If multiple master files are found in the same directory, they are processed in a unified manner. Therefore, useless master page files should not be placed in Web controls-they will be compiled anyway.
Process of generating the final page for the user
Any ASP. NET page bound to the master page must be constructed in a specific structure -- server controls or text cannot appear outside the <asp: content> tag. Therefore, the layout of the page looks like a set of content elements. Each element is bound to a different placeholder in the master page. The connection is established through the ID attribute. <Asp: content> the element acts as a control container. All tags are compiled into the template and associated with the attributes of the corresponding placeholders on the master page.
A master page is a special user control with a template area. The masterpage class inherits from the usercontrol class. Once instantiated as a user control, the master page will finally fill in the content of each template by defining the tag on the Content Page. Then, the final control is added to the Control tree on the current page. Shows the structure of the final page obtained by the user:
Nested master page
A master page can be associated with another master page to form a hierarchical and nested structure. If nested master pages are used, any child master pages should be implemented in the form of pure content, and additional contentplaceholder controls should also be defined for the Child content pages. That is to say, a child mother page is a special content page that contains a combination of <asp: content> and <asp: contentplaceholder> elements.
There is no architecture limit on the number of nested pages in the website. In terms of performance, the impact of nested depth on the scalability of overall functions and solutions can be ignored. As long as the source file on which the page depends is not changed, the page will only be compiled as needed, and the subsequently generated assembly can be reused.
Exposure of master page attributes
To identify the control on the master page, add the runat attribute to it and assign an ID to the control. But can I access this control on the Content Page? No. The only way to access the master page object is through the master attribute of the page class. We can only access public attributes and methods on the master page.
Sample Code:
public partial class SimpleWithProp : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{}
public string TitleBoxText
{
get{ return TitleBox.Text; }
set{ TitleBox.Text = value; }
}
}
Call of master page attributes
The page. Master attribute indicates the master page object generated by compiling the ASP. NET runtime database engine. This class is similar to a regular page, and its name also follows a naming convention-Asp. xxx_master, where xxx represents the name of the master page file. By setting the classname attribute of the @ master command, developers can override this default name. Sample Code:
<%@ Master Inherits="SimpleWithProp" Classname="MasterWithProp" %>
To call custom attributes or methods, we also need to convert the type of objects returned from the master attribute:
((ASP.MasterWithProp)Master).TitleBoxText = "Programming ASP.NET 3.5";
By adding the @ mastertype command on the content page, we can avoid the above type conversion. This command tells the compiler the True Type of the master attribute. As follows:
<%@ Page Language="C#" MasterPageFile="SimpleWithProp.master"
CodeFile="HelloMasterType.aspx.cs"
Inherits="HelloMasterType" %>
<%@ MasterType VirtualPath="SimpleWithProp.master" %>
protected void Page_Load(object sender, EventArgs e)
{
Master.TitleBoxText = "Programming ASP.NET 3.5";
}
The @ mastertype command supports two mutually exclusive attributes: virtualpath and typename. Both are used to identify the master page class. The former uses the URL, while the latter uses the type name.
Dynamic Changes to the master page
Masterpagefile pointing to the master page file on the page class is a readable/Write attribute. We can set this attribute only in the page preinit event to achieve dynamic changes to the master page.
Sample Code:
protected void Page_PreInit(object sender, EventArgs e)
{
MasterPageFile = "simple2.master";
}
If you try to set the masterpagefile attribute elsewhere, an exception is thrown.
After the file content referenced by the masterpagefile attribute is loaded, the master attribute is set by the runtime.