Asp.net 2.0 tutorial master page

Source: Internet
Author: User

Respect the author. Keep the words www.it55.com.

After May 1, I had a bad sweat and wanted to take a bath first. But at last, I decided to write the course today before doing other things.
This is often the case when we do websites: each page must have a header and a tail, and these headers or tails are the same for most of the time. repetitive work makes us very helpless. ASP programmers use include to solve this problem. Asp.net 1.x programmers use custom controls to reuse the public part. However, these methods have unavoidable drawbacks. Asp.net provides us with another new solution, namely, the master technology.
1. Simple master Application
Right-click the current project and choose> Add new project.

In the "Add new project" dialog box, select the master page and click "add"

VS has added a simple master page to the current project.
Make a simple modification to the master page. The final code is as follows:

<% @ Master language = "C #" autoeventwireup = "true" codefile = "masterpage. master. cs" inherits = "masterpage" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> simple master page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<P> I am the master page content </P>
<Div>
<Asp: contentplaceholder id = "contentplaceholder1" runat = "server">
</ASP: contentplaceholder>
</Div>
</Form>
</Body>
</Html>

Pay attention to the blue part of the code.

Next, we will add a web form file to the current project:
Right-click the current project and choose >>> add new project.
In the "Add new project" dialog box that appears, select a web form, select "select master page", and click "add ":

The "select master" Configuration box is displayed. Select the corresponding master under the corresponding project and click "OK ":

The system automatically generates the content page. The code is modified as follows:

<% @ Page Language = "C #" masterpagefile = "~ /Masterpage. Master "autoeventwireup =" true "codefile =" default. aspx. cs "inherits =" _ default "Title =" untitled page "%>
<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder1" runat = "server">
I am part of the content page.
</ASP: content>

Pay attention to the blue part of the code.
After running, you will see:

When we request the content page default. aspx, the system automatically applies the content of the master file to the content page and displays the final processing result to us. How does Asp.net 2.0 integrate master pages and content pages? The following is a simple process description:
The user requests the content page through the URL of the Content Page >>> after the system obtains the content page, obtain the corresponding master page by reading the masterpagefile attribute in the @ page command> the system merges the master page into the control tree of the content page. >>> Based on the ID of the "contentplaceholder" control on the master page and the "contentplaceholderid" attribute value of the content control on the content page, merge the content of each content control to the corresponding contentplaceholder control on the master page. >>> the system sends the merged result page to the client.

2. nested master Application
A nested master page is an advanced application of the master page function. It contains a small master page in a large master page. It should be noted that, no matter whether the master page is nested or has several nesting, the whole page architecture must contain at least one content page, because the master page itself cannot be accessed by users.
The following is the simple code for nesting the master page application:
Bigmasterpage. Master:

<% @ Master language = "C #" autoeventwireup = "true" codefile = "mainmasterpage. master. cs" inherits = "mainmasterpage" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div> big master page content </div>
<Div>
<Asp: contentplaceholder id = "contentplaceholder1" runat = "server">
</ASP: contentplaceholder>
</Div> # P # paging title # e #
</Form>
</Body>
</Html>

A master is the same as a simple application, and there is no change.

Small master page smallmasterpage. Master:

<% @ Master language = "C #" autoeventwireup = "true" codefile = "smallmasterpage. master. cs" inherits = "smallmasterpage" masterpagefile = "~ /Mainmasterpage. Master "%>
<Asp: Content ID = "subcontent" contentplaceholderid = "contentplaceholder1" runat = "server">
<Div> small master content </div>
<Div>
<Asp: contentplaceholder id = "contentplaceholder2" runat = "server">
</ASP: contentplaceholder>
</Div>
</ASP: content>

Here, it seems that the small master is actually a special content page with the master Mark. Only the file header, some programs, HTML code, and so on are allowed in the content control.

Content Page: content. aspx:

<% @ Page Language = "C #" masterpagefile = "~ /Smallmasterpage. Master "autoeventwireup =" true "codefile =" content. aspx. cs "inherits =" content "Title =" untitled page "%>
<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder2" runat = "server">
Content Page Content
</ASP: content>

The same is true for simple applications, but the master is smallmasterpage. master.
Let's take a look at the blue part. Running result:

3. Access Controls and properties on the master page
During program design, you may need to control the master page on the Content Page. To access the control or attribute of the master page, you can use the following methods:

1. Use the findcontrol method to obtain the reference of the master page control
Using the master public attribute of the page object on the Content Page, We can reference the associated master page. The findcontrol method of the master page is used to access the control of the master page.
Master page masterpage. Master:

<% @ Master language = "C #" autoeventwireup = "true" codefile = "masterpage1.master. cs" inherits = "masterpage1" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> master page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: Label runat = "server" id = "masterlabel"> content on the master page </ASP: Label>
<Div>
<Asp: contentplaceholder id = "contentplaceholder1" runat = "server">
</ASP: contentplaceholder>
</Div>
</Form>
</Body>
</Html>

Content Page content1.aspx:

<% @ Page Language = "C #" masterpagefile = "~ /Masterpage1.master "autoeventwireup =" true "codefile =" content1.aspx. cs "inherits =" content1 "Title =" untitled page "%>
<SCRIPT runat = "server">
Void page_loadcomplete (Object sender, eventargs E)
{
Contentlabel. Text = (master. findcontrol ("masterlabel") as label). text;
}
</SCRIPT>
<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder1" runat = "server">
<Asp: Label id = "contentlabel" runat = "server"> the masterlabel control on the master page is displayed. </ASP: Label>
</ASP: content>
"Page_loadcomplete" is an event triggered when the content page is loaded.
Running result:

2. Use the mastertype command to obtain the reference of the master page control
Compared with the findcontrol method above, mastertype is very direct. By using mastertype, you can create a strong type reference with the master page.

Change masterpage. Master in the findcontrol method example as follows:

<% @ Master language = "C #" autoeventwireup = "true" codefile = "masterpage1.master. cs" inherits = "masterpage1" %>
<SCRIPT runat = "server">
Public label masterpagelabel // note: the label control on the master page is strongly typed to facilitate access to the content page. This method is also used for access to the master page attributes.
{
Get # P # paging title # e #
{
Return masterlabel;
}
Set
{
Masterlabel = value;
}
}
</SCRIPT>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> master page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: Label runat = "server" id = "masterlabel"> content on the master page </ASP: Label>
<Div>
<Asp: contentplaceholder id = "contentplaceholder1" runat = "server">
</ASP: contentplaceholder>
</Div>
</Form>
</Body>
</Html>

Change content1.aspx in the findcontrol method example as follows:

<% @ Page Language = "C #" masterpagefile = "~ /Masterpage1.master "autoeventwireup =" true "codefile =" content1.aspx. cs "inherits =" content1 "Title =" untitled page "%>
<% @ Mastertype virtualpath = "~ /Masterpage1.master "%>
<SCRIPT runat = "server">
New void page_load (Object sender, eventargs E)
{
Contentlabel. Text = Master. masterpagelabel. text;
}
</SCRIPT>
<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder1" runat = "server">
<Asp: Label id = "contentlabel" runat = "server"> the masterlabel control on the master page is displayed. </ASP: Label>
</ASP: content>

4. Application Scope configuration on the master page
All the above instances are page-level master page applications. You only need to declare or set them in the header of the corresponding content page.
If it is a program-level master page application, you should make the following settings in Web. config:
<Configuration>
<System. Web>
<Pages masterpagefile = "~ /Masterpage. Master "/>
</System. Web>
</Configuration>
You can block a folder from using this master solution by placing different web. config files under this folder.
To shield a file from using this master scheme, you can set the. ASPX page header:
<% @ Page Language = "C #" masterpagefile = "~ /Othermasterpage. Master "%>
If you do not want to use the master, leave the masterpagefile property value blank.

Next section: New Features of Asp.net 2.0: Theme and skin

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.