master page

master page

Purpose of Master Pages

    It is to build the function of supporting web page templates from the inside to achieve the website consistency requirements.

Master page functionality (two includes, one combines)

    Two includes - refers to dividing the page into a non-public part and a public part, and both are included in two files respectively.

    A combination - refers to the behavior of control application and property setting, which organically combines the master page and the content page.

There are two types of files that must be included in the process of achieving site consistency:

  •     master page
            The extension of the master page is .master, which is the public element in its encapsulation page
  •     Content page

            The content is also actually a normal .aspx file which contains other non-public content besides the master page

Common master page code structure:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Master page and normal .aspx file code comparison

  •      The extension of the master page is .master, so the files with the extension of .master are master pages, which is different from ordinary .aspx files. The client browser can make a request to the server to access the .aspx file, but not if the request is for a master page. The client can access the content page, and the master page can be indirectly accessed only by binding the content page to the master page.
  •     The code header declaration of a normal .aspx file is <%@Page%>, while the code header declaration of a master page file is different, it must be declared as <%@Master%>. Other than that, there is basically no difference in code structure between master pages and ordinary .aspx files.
  •     A master page can contain one or more ConreolPlaceHolder controls. The control is not included in ordinary .aspx files. The ConreolPlaceHolder control acts as a placeholder that identifies an area in the master page that will be replaced by specific code in the content page.

Content pages mainly contain non-public content on the page. Although, its code structure is very different from ordinary .aspx files.

Common content page code structure:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>

The code of the content page is mainly divided into:

    (1) Code header declaration

    (2) Content control

The running process of content and master pages can be summarized as the following 5 steps:

    (1) The user requests a page by entering the URL of the content page.

    (2) After obtaining the content page, read the @Page instruction. If the directive refers to a master page, the master page is also read. If both pages are requested for the first time, both pages are compiled.

    (3) The master page is merged into the control tree of the content page.

    (4) The content of each Content control is merged into the corresponding ContentPlaceHolder control in the master page.

    (5) Present the resulting page.

Advantages of master pages

  •     It is beneficial to modify and maintain the site and reduce the work intensity of developers.
  •     Provide efficient content integration capabilities.
  •     Useful for page layout.
  •     Provides an easy-to-use object model.

Create a master page


   Create content pages

       



   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693233&siteId=291194637