ASP.NET website development -- master page

1. Overview of master pages

Each website needs to have a uniform style and layout, for example, the entire website has the same header and footer, navigation bar, function bar, and advertising area.

For this, this not only improves work efficiency, reduces development and maintenance intensity, but also provides strong support;

1.1, the advantages of master pages

Developers can take advantage of the master page feature to create a single-page layout and then apply it to multiple content pages:

1. It is beneficial to site repair and maintenance, and reduces the work intensity of developers;

2. Provide efficient content integration capabilities;

3, is conducive to the realization of page layout;

4. Provide an object model for convenience and utilization;

2.1, create a project

1. First create a web project

2. Add a new item: master page, named: ONE.Master

3. Add a master page Web form, ONE1.aspx


4,ONE.Master页:

<body>
    <form id="form1" runat="server">
    <div style="height:150px; background-color:Gray;" >
    top
    </div>
    <div style="width:1200px; margin:0 auto;">
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    <div style="height:150px; background-color:Gray;">
    bottom
    </div>
    </form>
</body>

Split into:

5. ONE1.aspx page:

If you want to change the appearance of the master in the ONE1.aspx page, you only need to:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>

Just add the code to it;

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div style="width:1200px; height:450px; background-color:Yellow; margin:0 auto;">
    </div>
</asp:Content>

operation result:

6. Add another master page Web form, which is ONE2.aspx, and also bind the master page: ONE.Master; ONE2.aspx page:

    <div style="width:1200px; height:450px; background-color:Aqua; margin:0 auto;">
    </div>

page effect:


7. Display the current page Method 1:


On the ONE.Master page add:

<asp:Label ID="Label1" runat="server" Text="当前页面:"></asp:Label>

On the ONE.Master.cs page add:

        public string Text
        {
            get
            {
                return Label1.Text;
            }
            set
            {
                Label1.Text += value;
            }
        }

ONE1.aspx page add:

<%@ MasterType VirtualPath="~/ONE.Master" %>

ONE1.aspx.cs page:

Master.Text = "ONE1.aspx";

8. Method 2:

Just add in the ONE2.aspx.cs page:

            if (!IsPostBack)
            {
                Label label = Master.FindControl("Label1") as Label;
                label.Text += "WebForm1.aspx";
            }
You can;


Guess you like

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