Master page in ASP. NET

Source: Internet
Author: User

What is a master page? As the name suggests, a master is a template. Just like a plate or a topic in a PPT, a large framework already exists. Our task is to add specific content to it. In this way, all the slides we make have the same appearance.


In ASP. NET, the master page has two functions: one is to improve code reuse (extract the same code), and the other is to maintain a consistent style and style for the entire website.
How to create and use a master page in? I will introduce it to you in detail below.
1. Add an item-master page


On the new master page, you will find that two ContentPlaceHolder controls are automatically generated.

 

One of them is in the head area, and the ID is "head"; the other is in the body area. The default ID is "ContentPlaceHolder1". You can name it as needed. These are two placeholder controls. In the future, the specific code will appear in the areas restricted by these two controls.

2. Add the public code to be extracted to the master page, and place the two ContentPlaceHolder controls to the places where the specific content needs to be added (the control in the general head area does not need to be moved, only the body zone is required .)

 


3. You can create a Web form using the master page (use the new master page as the master page)

 


In the new web form, you will find that there are also two controls, which are two Content controls (Content areas) and write specific code between them.

The Content control has a ContentPlaceHolderID attribute. Each Content control is associated with ContentPlaceHolder on the master page through this attribute.

One of the two controls is associated with the ContentPlaceHolder control in the head area of the master page, and the other is associated with the body area, as shown below:


[Html]
<! -- Code on the master page -->
<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]
<! -- Use the code on the web page of the master page -->
<Asp: Content ID = "Content1" ContentPlaceHolderID = "head" runat = "server">
</Asp: Content>
<Asp: Content ID = "Content2" ContentPlaceHolderID = "ContentPlaceHolder1" runat = "server">
</Asp: Content>


In this way, the association between a common webpage and a master page is realized through the ContentPlaceHolder control in the master page and the Content Control in the webpage of the master page.


What is the difference between a master page and a common web page?

1. The Master Page starts with <% @ Master... %>, while the normal Web Page starts with <% @ Page... %>

2. The master page cannot be previewed in the browser, but the normal page can.

3. Use the title of the web Page on the master Page to set <% @ Page ...... title = "title" %>, while the title of a general page is written in the <title> label of the head area

 

The master page can be nested, that is, the master page is created on the basis of the original master page. This not only further improves code reuse, but also ensures that all modules have their own sub-styles based on the consistent appearance of the entire website. For example, the overall appearance of a company's website is similar, and each department has its own sub-appearance.

The nested master page does not automatically generate the ContentPlaceHolder control and needs to be written manually.


Author: wwwwenhuan
 

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.