Implement homepage nesting in ASP. net2.0

Source: Internet
Author: User

Today, many commercial companies have different departments, and these departments have their own sub-websites on the company's website. Generally, each department maintains its own website based on its own needs. Although this will enrich the company's website, it will cause inconvenience to users, that is, because the sub-websites of various departments are not consistent, it is difficult for users to browse the website. Fortunately, ASP. net2.0 provides us with a solution, namely homepage nesting.

 Create a nested Home Page

First, you need to create a standard homepage. You need to add some shared items on this homepage, such as the company logo, company name, footer, and menu, the standard homepage must be used for sub-websites of each department. Each department can establish its own homepage based on its own business needs, and then embed the homepage of these departments into the standard just created. In this way, both the end user and the Department are good. For the end user, no matter which department he accesses, the website will see the same logo, menu, and footer. For departments, they can build an embedded home page based on their own business needs. This is like an OCX control embedded on a webpage.

To understand how to create a nested home page, let's look at an example below. First, an example of the standard homepage is provided.

The following is a reference clip:
<% @ Master language = "C #" autoeventwireup = "true"
Codefile = "websitemasterpage. master. cs" inherits = "websitemasterpage" %>
<HTML>
<Head runat = "server" id = "head">
<Title> standard homepage example </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
& Lt; table width = "100%" & gt;
<Tr>
<TD bgcolor = "#006633" colspan = "2">
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "100%">
<Tr>
<TD align = "Left">
<A href = "/masterpage/default. aspx">

</A>
</TD>
<TD align = "right">

</TD>
</Tr>
</Table>
</TD>
</Tr>
<Tr>
& Lt; TD width = "25%" & gt;
<Font color = "# 3300ff"> department 1 <br/> Department 2 <br/> </font>
</TD>
& Lt; TD width = "75%" & gt;
<Asp: contentplaceholder id = "Main" runat = "server">
</ASP: contentplaceholder>
</TD>
</Tr>
<Tr>
<TD colspan = "2"> </TD>
</Tr>
<Tr>
<TD bgcolor = "# 0000ff" colspan = "2">
<Font color = "# FFFF00"> note </font>
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>

The above standard homepage defines the company's logo, footer, and menu location. It also defines the location of the department's home page to be embedded (this requires the contentplaceholder control ). Department HomepageCodeUnlike the above Code, the above standard homepage must be referenced in the code of the Department Homepage. You can add the masterpagefile attribute to the Department Homepage code. The following is the homepage code of a department:

The following is a reference clip:
<% @ Master masterpagefile = "~ /Templates/websitemasterpage. Master "Language =" C # "autoeventwireup =" true "codefile =" nestedmasterpage. master. cs "inherits =" nestedmasterpage "%>
<Asp: Content ID = "content1" contentplaceholderid = "Main" runat = "server">
& Lt; table width = "100%" & gt;
<Tr>
<TD style = "background-color: Blue; font-weight: bold; coloar: White">
<Font color = "# FFFF00"> Department homepage </font>
</TD>
</Tr>
<Tr>
<TD>
<Asp: contentplaceholder id = "nestedmain" runat = "server"/>
</TD>
</Tr>
</Table>
</ASP: content>

From the code above, we can see that the standard homepage websitemasterpage. Master is referenced. A server control is also defined to reference the contentplaceholder ID (ID is main) defined on the standard homepage ). Because the Department Homepage is nested in the standard homepage, you must use the Content Server Control. The contentplaceholder control must be added to indicate the position displayed on the Department Homepage.

Now this Department homepage has been embedded into the standard homepage. The Department's homepage automatically inherits the logo, footer, and menu of the standard homepage. To replace these common elements, you only need to update the standard homepage. In addition, each department can update the Department Homepage embedded on the standard homepage as needed.ProgramRun interface 1.

 

Figure 1

Use nested homepage in visual studio2005

 

 

We can see from the code on the Department Homepage that the masterpagefile attribute references the standard homepage. However, this attribute does not support visual editing in visual studio2005. Therefore, to edit the homepage in the design view of vs2005, you must set masterpagefile as an empty string. The following code is shown:

 

The following is a reference clip:
<% @ Page Language = "C #" masterpagefile = "" Title = "Department Homepage" %>

 

When masterpagefile is set as an empty string, you must manually modify this attribute each time you update the standard homepage. If you don't want to be so troublesome, you can use some means to fool a Visual Studio. NET design view. First, create a class inherited from system. Web. UI. Page (name this class basepage ). Define a runtimemasterpagefile attribute in this class (this attribute can use any name ). The implementation code is as follows:

The following is a reference clip:
Public class basepage: system. Web. UI. Page {
Private string _ runtimemasterpagefile;

Public String runtimemasterpagefile {
Get {
Return _ runtimemasterpagefile;
}
Set {
_ Runtimemasterpagefile = value;
}
}

Protected override void onpreinit (eventargs e ){
If (_ runtimemasterpagefile! = NULL ){
This. masterpagefile = _ runtimemasterpagefile;
}
Base. onpreinit (E );
}
}

Basepage also reloads the onpreinit method so that the masterpagefile attribute can be dynamically set during ASPX page loading. After the basepage class is implemented, classes in the embedded aspx can be inherited directly from the basepage.

 

 

The following is a reference clip:
Public partial class mynestedmaster: basepage {

// Specific implementation code
}

 

Next we will modify the. aspx file. Set the masterpagefile attribute to an empty string, add the runtimemasterpagefile attribute to the aspx file, and set the value to the path of the embedded homepage. Set the codefilebaseclass attribute to "basepage". The implementation code is as follows:

The following is a reference clip:
<% @ Page Language = "C #" masterpagefile = ""
Runtimemasterpagefile = "~ /Templates/nestedmasterpage. Master"
Codefilebaseclass = "basepage"
Inherits = "mynestedmasterandbasepage" autoeventwireup = "true"
Codefile = "mynestedmasterandbasepage. aspx. CS Title =" page1 "%>

<Asp: Content ID = "contentnested" runat = "server" contentplaceholderid = "nestedmain">
<P> & nbsp; </P>
Finance Department Homepage
<P> & nbsp; </P>
</ASP: content>

At runtime, The basepage class will be instantiated, And the masterpagefile attribute will be dynamically set to the value of the runtimemasterpagefile attribute.

 

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.