Original: Implement home page nesting in asp.net2.0

Source: Internet
Author: User
Tags empty implement modify reference visual studio
Asp.net| Original

Many commercial companies now have different departments, which have their own subsites on the company's website. In general, each department will maintain its own website according to its own needs. While this will make the company's Web site rich and colorful, but this will bring inconvenience to users, that is, because the various departments of the subsites are not consistent, so that users in browsing the site caused difficulties. Luckily, Asp.net2.0 provides us with a solution, which is the home page nesting.

Building a nested home page

First you need to set up a standard home page where you need to add some common items such as company logos, company names, footers, and menus, and each department's subsite must use this standard home page. Each department can build its own home page based on its own business needs, and then embed the homepage of these departments into the standard just established. This is good for the end user or for each department, and for the end user, no matter which department they visit, the same logo, menu, and footer will be seen. For the department, they can build a built-in home page that can be based on the business needs of their department. It's like an OCX control embedded on a Web page.

To understand how to build a nested home page, let's look at an example. First, give an example of a standard home page.

The following is a reference fragment:
<%@ Master language= "C #" autoeventwireup= "true"
Codefile= "WebsiteMasterPage.master.cs" inherits= "Websitemasterpage"%>
<title> Standard Homepage Example </title>
<body>
<form id= "Form1" runat= "Server" >
<table width= "100%" >
<tr>
&LT;TD bgcolor= "#006633" colspan= "2" >
<table border= "0" cellpadding= "0" cellspacing= "0" width= "100%" >
<tr>
&LT;TD align= "Left" >
<a href= "/masterpage/default.aspx" >

</a>
</td>
&LT;TD align= "Right" >

</td>
</tr>
</table>
</td>
</tr>
<tr>
&LT;TD width= "25%" >
<font color= "#3300FF" > Department 1 <br/> Department 2 <br/></font>
</td>
&LT;TD width= "75%" >
<asp:contentplaceholder id= "Main" runat= "Server" >
</asp:ContentPlaceHolder>
</td>
</tr>
<tr>
&LT;TD colspan= "2" > </td>
</tr>
<tr>
&LT;TD bgcolor= "#0000FF" colspan= "2" >
<font color= "#FFFF00" > Footnotes </font>
</td>
</tr>
</table>
</form>
</body>

The standard homepage above defines the location of the company's logo, footnotes, and menus. Also defines where the department's home page will be embedded (this will use the ContentPlaceHolder control). The code in the Department home page is different from the code above, and you need to refer to the standard home page in the code for the department home page. This can be done by adding the MasterPageFile property to the Department homepage code. The following is the homepage code for a department:

The following is a reference fragment:
<%@ Master masterpagefile= "~/templates/websitemasterpage.master" language= "C #" autoeventwireup= "true" CodeFile= " NestedMasterPage.master.cs "inherits=" Nestedmasterpage "%>
<asp:content id= "Content1" contentplaceholderid= "Main" runat= "Server" >
<table width= "100%" >
<tr>
&LT;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>

You can see from the above code that the standard homepage websitemasterpage.master is referenced. A server-side control is also defined to refer to the ID of the ContentPlaceHolder defined in the standard home page (ID is main). Because the Department home page is nested within the standard home page, the content server-side control must be used. There is also the need to join the ContentPlaceHolder control, which indicates where the department's home page is displayed.

Now this department homepage has been embedded in the standard home page. The Department's homepage can automatically inherit the logos, footnotes, and menus of the standard home page. If you want to replace these common elements, just update the standard home page. Also, departments can update their own departmental home pages embedded in the standard home page according to their needs. The program runs the interface as shown in Figure 1.

Figure 1

using nested home pages in Visual Studio2005

  We can see from the above Department homepage code that the MasterPageFile property refers to the standard home page. However, this property does not support visual editing in Visual Studio2005. Therefore, to edit the home page in VS2005 Design view, you must set the MasterPageFile to an empty string. As shown in the following code:

<%@ page language= "C #" masterpagefile= "title= Department home Page"%>

When we set the MasterPageFile to an empty string, we have to manually modify this property at the time of publication after each update of the standard home page. If you don't want to be so troublesome, you can trick a visual Studio by some means. NET Design view. First, create a class that inherits from System.Web.UI.Page (name this class basepage). Define a Runtimemasterpagefile attribute in this class (this property can use any name). The implementation code is as follows:

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 overloads the Onpreinit method so that the MasterPageFile property can be set dynamically when an ASPX page is loaded. After the BasePage class is implemented, classes in ASPX that need to be embedded in the future can be inherited directly from BasePage.

Public partial class Mynestedmaster:basepage {

The specific implementation code
}

    below we modify the. aspx file. First set the MasterPageFile property to an empty string, and add the Runtimemasterpagefile property directly to the ASPX file and set its value to the path of the inline home page. Then set the CodeFileBaseClass property to "BasePage", the implementation code is as follows:

<%@ page la Nguage= "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>  </p > <p>  </p>
    Finance Department Home
<p>  </p>  <p>  </p>
</asp:content>

at runtime, the BasePage class is instantiated, and the MasterPageFile property is dynamically set to the value of the Runtimemasterpagefile property.



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.