ASP. Net skillfully uses the Form Master page instance and asp.net form instance

Source: Internet
Author: User

ASP. Net skillfully uses the Form Master page instance and asp.net form instance

This example describes how ASP. Net uses the Form Master page. Share it with you for your reference. The specific analysis is as follows:

Background: the Basic Framework Structure of each webpage is similar:

When you browse a website, you will find that in many websites, the basic framework of each webpage is the same. For example, the top part is the website title and the middle part is the content, the following are website copyrights, development providers, and other information:

In these webpages, the style and content at the header and bottom are the same. The difference is only the intermediate content.

Therefore, when creating a website, you can separate these common items and place them on the Form Master page. You can nest them as needed.

Skillfully use the form master item:

Next we will start to take action (this article uses VisualStudio2013 as the programming environment, and may be different from other versions in some steps, please note ):

1. Add a Web form Master page test. Master to the project: Right-click the project and choose add> new project> Web form Master page;
Copy codeThe Code is as follows: <% @ Master Language = "C #" AutoEventWireup = "true" CodeBehind = "test. master. cs" Inherits = "Web. test1" %>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> </title>
<Asp: ContentPlaceHolder ID = "head" runat = "server">

<Form id = "form1" runat = "server">
<Div>

</Asp: contentplaceholder>
</Div>
</Form>
<! -- Html>

2. Add CSS, JS, and other references to the mark of test. Master on the Form Master page (here, only the CSS file is added as an example ):
Copy codeThe Code is as follows: <link href = "css/common.css" rel = "stylesheet"> <% -- add reference CSS file -- %>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> </title>
</Asp: contentplaceholder>
3. Edit the test. Master page of the form and add the public content of each webpage. (the layout of the webpage is used as an example. The css style of the three divs is not described at the moment ):
Copy codeThe Code is as follows: <form id = "form1" runat = "server">
<Div id = "top"> <% -- Public style of each webpage: webpage header -- %>
<H1> A certain website </Div>
<Div id = "main"> <% -- different webpage styles: webpage subject content -- %>
<% -- Here is the different content of each webpage nested with this master -- %>
</Asp: contentplaceholder>
</Div>
<Div id = "footer"> <% -- Public style of each webpage: webpage copyright information zone -- %>
<P> copyright: ****** </p>
</Div>
</Form>

4. nest the Form Master Page test in each webpage. master: Right-click the project and choose add> new project> Web form test that contains the Master page. aspx. In the select master page dialog box, select test. master, OK. The generated webpage is:
Copy codeThe Code is as follows: <% @ Page Title = "\" Language = "C #" MasterPageFile = "~ /Common. Master "AutoEventWireup =" true "CodeBehind =" test2.aspx. cs "Inherits =" Web. test2 "%>
</Asp: content>
</Asp: content>
In this case, the form test. aspx has the same running effect as the Master page test. Master. The next step is to add different content in each webpage.

5. In this case, the ContentPlaceHolderID = "head" and ContentPlaceHolderID = "contentPlaceHolder" in the test. aspx page are equivalent to those on the Master page test. Master. Therefore, if each web page has the same part, you can write the same part in the corresponding location of the master page, and write the different content of each web page in the ContentPlaceHolderID = "contentPlaceHolder.

For example, in step 1, this test. aspx has nested this style, and its subject content is "website content ...... ", The code in test. aspx is:
Copy codeThe Code is as follows: <% @ Page Title = "\" Language = "C #" MasterPageFile = "~ /Common. Master "AutoEventWireup =" true "CodeBehind =" test2.aspx. cs "Inherits =" Web. test2 "%>
</Asp: content>
<P> website content ...... </P>
</Asp: content>
If I create another webpage named test1.aspx, except for the content of the subject in test1.aspx, which is the same as the content of the other webpage, we can let test1.aspx nest the Master page test. Master with the code:
Copy codeThe Code is as follows: <% @ Page Title = "\" Language = "C #" MasterPageFile = "~ /Common. Master "AutoEventWireup =" true "CodeBehind =" test2.aspx. cs "Inherits =" Web. test2 "%>
</Asp: content>
<P> website content 1 website content 1 website content 1 website content 1 ...... </P>
</Asp: content>

Extended: master page nesting master page

When all the webpages on the website are not in this style, a large website is divided into several sections, and the webpage style in each section is unified, at this time, you can use the master page to nest the master page:

Now create a Master page (I will name it the "Child Mother page") and use it to nest the Master page test. Master above. The code is:
Copy codeThe Code is as follows: <% @ Master Language = "C #" MasterPageFile = "~ /Test. Master "AutoEventWireup =" true "CodeBehind =" m_common.master.cs "Inherits =" Web. admin. m_common "%>
</Asp: content>
<% -- The content of each page nested with the "parent page" -- %>
</Asp: contentplaceholder>
</Asp: content>
Note: MasterPageFile = "~ /Test. Master is the address of the Master page to be nested. Is the current directory.

Benefits of nested webpage master items:

You can use the form master page in Visual Studio to encapsulate the same part of each page. When creating a webpage with the same structure, you can directly nest this Form Master page, this avoids code duplication and improves code reusability. In addition, if you want to modify the style of a topic or even the entire website, you only need to modify the master page and the file <script> introduced in the master page to improve the maintainability of the code and fully reflect the idea of OOP.

I hope this article will help you design your asp.net program.

Related Article

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.