Asp. NET skillfully using form Master pages

Source: Internet
Author: User

Background: The basic frame structure for each page is similar:

Browsing the site will find that a lot of web sites, the basic framework of each page is the same, for example, the top is the title of the site, the middle is the content, the bottom is the site's copyright, development providers and other information:

In these pages, the header, the bottom of the style and content are the same, the difference is only the middle content.

So when making a website, you can separate these things together and put them in the "Form Master page" and nest them when you need them.


To use the form Master item skillfully:

Proceed as follows (this article is based on VisualStudio2013 as a programming environment and may differ in some steps from other versions, please note yourself):

1. Add a Web Forms master page to the project Test.master: Right-click Project-Add-new Item-web form master page;

<%@ Master language= "C #" autoeventwireup= "true" codebehind= "Test.master.cs" inherits= "Web.test1"%><! DOCTYPE html>

2. On the Form master page, test. add css, JS and other references between the

3. Edit Form master Page teSt. Master, add the common content for each page (for example, the layout of the page layout as a case, the CSS style of the three Div is temporarily not explained):

<body>    <form id= "Form1" runat= "Server" >        <div id= "Top" >                                 <%--Common styles for each Web page: page Header--%>            

4. Nesting the form master page in each pagetest. Master: Right-click Project-Add-new Item-Web Form test.aspx that contains the master page, select T est in the Select Master Page dialog box. Master, OK, the resulting web page is:

<%@ page title= "" Language= "C #" masterpagefile= "~/common. Master "autoeventwireup=" true "codebehind=" Test2.aspx.cs "inherits=" Web.test2 "%><asp:content id=" Content1 " Contentplaceholderid= "Head" runat= "server" ></asp:content><asp:content id= "Content2" Contentplaceholderid= "ContentPlaceHolder" runat= "Server" ></asp:Content>
at this point the form test.aspx and the master page teSt. Master works the same way, and the next step is to add a different content to each page.

5, at this time, the page test.aspx contentplaceholderid= "Head" and contentplaceholderid= "ContentPlaceHolder" <asp:content > is equivalent to the master page test. The corresponding <asp:Content> in master . So if each page will have the same part, you can write the same part in the corresponding location of the master page, and write the different contents of each page in contentplaceholderid= "ContentPlaceHolder" <asp:content > in.

For example, in the 4th step, this test.aspx has nested this style, its theme is "Site content site content Site content site contents ... ", then The code in Test.aspx is:

<%@ page title= "" Language= "C #" masterpagefile= "~/common. Master "autoeventwireup=" true "codebehind=" Test2.aspx.cs "inherits=" Web.test2 "%><asp:content id=" Content1 " Contentplaceholderid= "Head" runat= "server" ></asp:content><asp:content id= "Content2" Contentplaceholderid= "ContentPlaceHolder" runat= "Server" > <p> site Content site content site contents website content ...    </p></asp: Content>
If I have a Web page called test1.aspx, other than the main content in Test1.aspx, the other is the same, then you can let test1.aspx nested master page test. Master, the code is:

<%@ page title= "" Language= "C #" masterpagefile= "~/common. Master "autoeventwireup=" true "codebehind=" Test2.aspx.cs "inherits=" Web.test2 "%><asp:content id=" Content1 " Contentplaceholderid= "Head" runat= "server" ></asp:content><asp:content id= "Content2" Contentplaceholderid= "ContentPlaceHolder" runat= "Server" >    <p> Site content 1 site content 1 site content 1 site content 1 ... </p>< /asp:content>

    

Expand: Master Page nested master page

When all the pages in the entire site are not this style, but a large site is divided into several columns, each column of the style of the page is unified, this time you can use the master page to nest the master page:


now create a master page (I'll name it "Child master page" here) and use it to nest the master page test above. Master, the code is:

<%@ Master language= "C #" masterpagefile= "~/test. Master "autoeventwireup=" true "codebehind=" M_common.master.cs "inherits=" Web.admin.m_common "%><asp:content Id= "Content1" contentplaceholderid= "Head" runat= "Server" >    </asp:content><asp:content id= "Content2 "Contentplaceholderid=" ContentPlaceHolder "runat=" Server ">        <asp:contentplaceholder id=" Contentplaceholder_child "runat=" server >         <%--here is a different content--%> </asp for individual pages nested "child master Pages"    : Contentplaceholder></asp:content>
Note that the first line of the code is masterpagefile= "~/test. Master is the address of the master page to be nested, and "~" is the current directory.


Benefits of nesting page master items:

You can use the form master page in VisualStudio to encapsulate the same parts of each page, and when you create a Web page of the same structure, you can nest the form master page directly, avoiding duplication of code and improving the reusability of the Code; If you want to modify the style of a column or even the entire site, you only need to modify the master page and the master page introduced in the <script> and other files, improve the maintainability of the Code, fully embodies the idea of OOP.

Asp. NET skillfully using form Master pages

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.