Use ASP. NET master page

Source: Internet
Author: User

From: http://blog.csdn.net/cxzhq2002/archive/2008/12/17/3539685.aspx

<% @ Page Language = "C #" masterpagefile = "~ /Masterpage/common. Master "autoeventwireup =" true "codefile =" scenelist. aspx. cs "inherits =" scene_scenelist "Title =" Shaoguan tourism com-Scenic Spot list "%>

 

This. master. Page. header. Title = "AA ";
This. Page. Title = "BB ";

 

Generally, the content page does not need a CS file. It is a separate file and then user_control is used.

 

==================================

1. Set title

1. If you want to use a title for all pages using masterpage, you can set the title on the masterpage page and remove the title from the content page, otherwise, the title on the Content Page overwrites the title in masterpage.

<% @ Page Language = "C #" masterpagefile = "~ /Masterpage. Master "autoeventwireup =" true "codefile =" masterpagetest. aspx. cs "inherits =" masterpagetest "Title =" test "%>
Change

<% @ Page Language = "C #" masterpagefile = "~ /Masterpage. Master "autoeventwireup =" true "codefile =" masterpagetest. aspx. cs "inherits =" masterpagetest "%>
2. If you want to use different titles on each page, you can set them on the content page. The title in masterpage does not need to be taken care of. It should eventually be overwritten by the content page.

 

2. Obtain the attributes and fields in masterpage on the Content Page.

In the content page, getting the attributes or fields in masterpage should be more common. Create a masterpage mastertest. Master and Content Page Test. aspx. Add an attribute to the mastertest. Master background code, as shown below:

Private string m_name;

Public string name

{

Get {return m_name ;}

Set {m_name = value ;}

}

 

Then, in the background code of the content page, you will find that you cannot access the attributes in masterpage. In this case, switch to the source of the Content Page and add

 

<% @ Mastertype virtualpath = "~ /Mastertest. Master "%>
Switch to the background to access the masterpage attributes.

Scope of action of the three masterpages

1 Page Level

In general, masterpage is used at the page level, that is, the masterpage name is specified in each content page and set through the mastpagefile attribute, as shown below:

<% @ Page Language = "C #" masterpagefile = "~ /Masterpage. Master "%>
2 application-level

In this case, you only need to configure the content in the webconfig file. All content pages of the whole site reference the configured masterpage. For example, add the following code to webconfig:

<Configuration>
<System. Web>
<Pages masterpagefile = "~ /Test. Master "/>
</System. Web>
</Configuration>
In this way, you do not need to set the mastpagefile attribute in the content page. All content pages use test. master. If other masterpages are needed for some pages, the mastpagefile attribute will overwrite the configurations in webconfig.

In this way, you can also set all the files in some folders. The configuration is as follows:

<Configuration>
<Location Path = "admin">
<System. Web>
<Pages masterpagefile = "~ /Test. Master "/>
</System. Web>
</Location>
</Configuration>
Path attribute setting path of location

 

======================================

ASP. NET master page change content page title Method

After the master page is defined, sometimes we need to change the title of the webpage. However, if we change the title attribute directly on the master page, the same title may occur on other content pages, vs2008 provides new features for the master page.
1. Use the title attribute in the page command on the Content Page to change the title of the Content Page:

<% @ Page Language = "C #" masterpagefile = "~ /Mymaster. Master "Title =" my title "%>
2. Change through programming: The premise is that the

Void page_load ()
{
......
Page. header. Title = "my title ";
......
}
3. Through the head placeholder control on the content page, the master page added in vs2008 will have the following ASP: contentplaceholder controls in the header (drag the title label of the master page to the control)

<Asp: contentplaceholder id = "head" runat = "server">
<Title> No title page </title>
</ASP: contentplaceholder>
On the content page, a corresponding ASP: content control is often added. You only need to change the title label content.

<Asp: Content ID = "content1" contentplaceholderid = "head" runat = "server">
<Title> No title page </title>
</ASP: content>

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.