ASP. NET implement replacing MasterPage with PageBase

Source: Internet
Author: User

ASP. NET uses the. cs file in MasterPage to replace PageBase in the project.

The motivation for writing this article comes from a project reconstruction. In. in the B/S architecture project of Net Framwork 2.0, the PageBase and MasterPage technologies are used at the same time to find that accessing PageBase and MasterPage at the same time not only reduces the performance, it may even cause a logical error to the function expansion and adjustment of the project in the future.

PageBase: A technology often used in. Net Framework 1.1 to encapsulate the same functions of multiple pages. PageBase. cs class continues from System. web. UI. page class, the project's Web Page continues from PageBase. cs class. By Rewriting the page Initialization Method in the base class, you can call the business functions in PageBase. For example, for url parameter verification and traffic storage functions, see the official Microsoft example duwamishi ).

MasterPage: a new feature in. Net Framework 2.0. It physically includes two files:. Master File Html Tag) and. cs file C # code ).. The Master file is used to draw the display layer, and the. cs file implements specific functions. You can continue the content of the display layer in MasterPage from the Web page of MasterPage. Drawing a general page header and footer, and customizing a uniform layout, MasterPage is a good choice.

Use MasterPage technology to replace PageBase and ASP. NET to verify the address bar parameters.
A simple explanation

After logging on to the system, the url address bar contains parameters:
Http: // localhost: 3730/MasterPageBaseDemo/TestPage. aspx? Id = 1001
In this case, manually modify the parameters in the url address bar as follows:
Http: // localhost: 3730/MasterPageBaseDemo/TestPage. aspx? Id = 1002
The system will automatically jump back to the logon page.

The traditional Page method is as follows:

 
 
  1. PublicclassPageBase: System. Web. UI. Page
  2. {
  3. PublicPageBase ()
  4. {
  5. }
  6. /**////<Summary> 
  7. /// Entry Method
  8. /// Summary> 
  9. ProtectedvoidInitialize ()
  10. {
  11. // Insert general business logic
  12. }
  13. }

Web page:

 
 
  1. PublicpartialclassTestPage: PageBase
  2. {
  3. // The traditional method of calling PageBase
  4. /**/////<Summary> 
  5. /// Override the OnPreInit () method of the base class and call the common Verification Method
  6. /// Summary> 
  7. ///<ParamnameParamname="E"> Param> 
  8. Protectedoverrisponidoninit (eventargse)
  9. {
  10. Base. Initialize ();
  11. }
  12. }

Follow these steps to move the code in PageBase to MasterPage:
MasterPage. cs:

 
 
  1. PublicpartialclassMyMasterPage: System. Web. UI. MasterPage
  2. {
  3. ProtectedvoidPage_Load (objectsender, EventArgse)
  4. {
  5. If (! IsPostBack)
  6. {
  7.  
  8. // Call the Verification Method
  9. Initialize ();
  10. }
  11. }
  12. }

Modify the code on the Web page:

 
 
  1. PublicpartialclassTestPage: System. Web. UI. Page
  2. {
  3. // Call the method in Master following the PageBase Method
  4. /**////<Summary> 
  5. /// Override the OnPreInit () method of the base class and call the common Verification Method
  6. /// Summary> 
  7. ///<ParamnameParamname="E"> Param> 
  8. Protectedoverrisponidoninit (eventargse)
  9. {
  10. // Obtain the parent page reference
  11. MyMasterPagemyMasterPage= (MyMasterPage) this. Master;
  12. // Call the common verification method on the motherboard page
  13. If (! IsPostBack)
  14. {
  15. MyMasterPage. Initialize ();
  16. }
  17. }
  18. }

The preceding sections describe PageBase and MasterPage of ASP. NET.

  1. Implementation of ASP. NET plug-in
  2. Overview ASP. NET Applications
  3. Introduction to ASP. NET 2.0 Data Binding
  4. ASP. NET prevents Java Script Injection attacks
  5. ASP. net mvc using T4

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.