How to Use masterpage

Source: Internet
Author: User

How to Use masterpage

1. Create a masterpage with the suffix. Master, for example, X. master.
<Asp: contentplaceholder/> is used to define the vacancy. For example:

<Asp: contentplaceholder id = "contentplaceholder1" runat = "server">
</ASP: contentplaceholder>

2. Create a content page.
In the newitem dialog box, select "select master page" and select the masterpage created in the previous step.
In the generated code, the masterpagefile attribute specifies the masterpage location:

<% @ Page Language = "VB" masterpagefile = "~ /X. Master "Title =" untitled page "%>

Use <asp: Content/> to add content to the corresponding blank space on the page:

<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder1" runat = "server">
Content
</ASP: Content/>

The content page does not contain <Form ID = "form1" runat = "server">

3. MasterPage can be used to compile each part of a page in multiple languages.

4. In addition to specifying MasterPage in <% @ Page %>, you can also specify MasterPage in web. config:

<Configuration>
<System. web>
<Pages masterPageFile = "~ /X. master "/>
</System. web>
</Configuration>

After this definition, if you select master Page when creating a page, you can use this MasterPage without specifying it in <% @ Page %>.
To use different masterpages for other pages, you only need to use the first method to explicitly overwrite the settings in web. config in Page ctive ve.

You can only specify MasterPage for a group of pages. The following example uses the location element of web. config to set different masterpages for pages under the Admin directory.

<Configuration>
<Location path = "Admin">
<System. web>
<Pages masterPageFile = "~ /Y. master "/>
</System. web>
</Location>
</Configuration>

5. On the content Page, how do I set the Title of the Page?

By default, this Title is used on other pages after the Title is specified in MasterPage.
On the specific page, you can modify the Title in two ways:

A. <% @ Page Title = "test" %>

B. In the Code:

Protected void Page_LoadComplete (object sender, EventArgs e)
{
Master. Page. Title = "Hello ";
}


6. Access the properties and controls in MasterPage.

Use the Master attribute for access.

A. Assume that there is a Label1 in MasterPage, the content page can be as follows:

Protected void Page_LoadComplete (object sender, EventArgs e)
{
String text = (Master. FindControl ("Label1") as Label). Text;
}

Order of page loading:

To obtain the value set in Page_Load of MasterPage, it must be written in Page_LoadComplete of the Content Page.

The FindControl () method mentioned above is used to find controls in MasterPage. It is a later binding method and is generally insecure. Because it depends on whether the tag exists in MasterPage. If it is deleted, it will cause an error.
A good practice is to encapsulate access to its control with properties in MasterPage; If FindControl () is used, it always checks whether the result is null.

7. Specify the default content in MasterPage

You can directly specify between <asp: ControlPlaceHolder/> labels.
If the sub-page is not re-specified, the default content is used.

8. Specify the Master Page by programming

Protected void Page_PreInit (object sender, EventArgs e)
{
Page. MasterPageFile = "~ /X. master ";
}

9. nested Master Page

The Master Page can inherit from a higher level of Master Page. However, it is not supported by default when this seed Master Page is created in VS2005.
Suppose there is A. master,
Create a general B. master first,
Delete the other parts except Page ctictive.
Modify Page ctictive to the following and add your own PlaceHolder:

<% @ Master MasterPageFile = "~ /A. master "%>

<Asp: Content ID = "Content1" ContentPlaceHolderID = "ContentPlaceHolder1" Runat = "server">
Hello!
<Asp: ContentPlaceHolder ID = "ContentPlaceHolder2" Runat = "server">
</Asp: ContentPlaceHolder>
</Asp: Content>

Child pages generated by nested templates cannot adopt the VS2005 design mode.

10. Container-specific Master Pages

To be compatible with different browsers, asp.net 2.0 supports multiple Master pages. The appropriate Master pages are automatically loaded at runtime.

Syntax:

<% @ Page Language = "VB" MasterPageFile = "~ /Abc. master"
Mozilla: masterpagefile = "~ /Abcmozilla. Master"
Opera: masterpagefile = "~ /Abcmozilla. Master "%>

11. Order of page requests

When a user requests a page built on the master page, the order of events is as follows:

Master page subcontrol initialization;
Content Page subcontrol initialization;
Master page initialization;
Content Page initialization;
Content Page page_load;
Page_load of the master page;
Master page sub-control loading;
Content Page sub-control loading;


Note:

Because the page_load of the content page is prior to the page_load of the master page, to access the server control in the master page, you must write code in the page_loadcomplete method of the Content Page.

12. Use Cache

You can use the following Directive to specify the cache only on the Content Page:

<% @ Outputcache duration = "10" varybyparam = "NONE" %>

(This command allows the server to cache the page in memory for 10 seconds)

If this command is specified on the master page, no error is thrown. However, when his sub-page obtains the master page next time, if the master page has expired, an error is thrown.
Therefore, you can only specify the cache for the Child page.

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.