ASP. net1.1 achieves the effect of imitating 2.0 masterpage

Source: Internet
Author: User
In Asp.net 2.0, A masterpage function is provided, which allows us to easily complete the website with the same overall structure as the page. In addition, you only need to modify the masterpage when modifying the page later, you do not need to modify the interface one by one, which greatly facilitates developers. in fact, similar technology in ASP. net1.1 can also be implemented.

I. Train of Thought Analysis
For websites that can use mastetrpage, the page structure is mostly the same, for example, the top and bottom are the same, but the content displayed on the left and right is different. At this time, you may wish to place the content on both sides of the left and right in the user control, and then dynamically load the user control according to the current access address, in this way, you only need to modify the content of the dynamically loaded user control or the masterpage framework to repair the website.

II. Implementation Process
First, obtain the file address currently accessed,CodeAs follows:

// Obtain the current access path
String strrequesturl = request. url. absolutepath. tostring ();
// Get the name of the currently accessed file and convert it to lowercase
String strnowviewfile = strrequesturl. substring (strrequesturl. lastindexof (@ "/") + 1). Trim (). tolower ();

Then click "add? Add a Web user control. Create three new user controls named masterpage. ascx, head. ascx and foot. ascx, in masterpage. add a table with three rows and two columns in ascx, and then set the header. ascx and foot. drag the ascx control to masterpage. in ascx, a placeholder control is added to both left and right sides, respectively named hldleftcontrol and hldrightcontrol. The effect is as follows:

Head. ascx

Hldleftcontrol

Hldrightcontrol

Foot. ascx

In this case, you only need to drag masterpage. ascx to a specific page, such as index. aspx. After running the command, the content at the top and bottom is displayed. Next, determine which user controls are dynamically loaded to the left and right sides based on the current file address. This operation is completed in masterpage. ascx. CS. The Code is as follows:

Switch (strnowviewfile)
{
// If the current homepage is accessed
Case "index. aspx ":
// Create an object for the user control class. The class name is the same as the user control name.
Control_index_indexrightcontrol indexrightcontrol;
// Load the user control
Indexrightcontrol = (control_index_indexrightcontrol) page. loadcontrol ("control_index_indexrightcontrol.ascx ");
// Display the user control in the placeholder control on the right
Plhrightcontrol. Controls. Add (indexrightcontrol );
Break;
// Load Member registration
Case "register. aspx ":
Control_login_register register;
Register = (control_login_register) page. loadcontrol ("control_login_register.ascx ");
Plhrightcontrol. Controls. Add (Register );
Break;
Default:
Indexrightcontrol = (control_index_indexrightcontrol) page. loadcontrol ("control_index_indexrightcontrol.ascx ");
Plhrightcontrol. Controls. Add (indexrightcontrol );
Break;
}

After the above Code is added, if the accessed page is index. aspx, in index. on the Right of the ASPX page, the content of the control_index_indexrightcontrol.ascx control is loaded. on the ASPX page, the content of the control_login_register.ascx control is loaded on the right. in this way, the masterpage function is implemented.

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.