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:
- PublicclassPageBase: System. Web. UI. Page
- {
- PublicPageBase ()
- {
- }
- /**////<Summary>
- /// Entry Method
- /// Summary>
- ProtectedvoidInitialize ()
- {
- // Insert general business logic
- }
- }
Web page:
- PublicpartialclassTestPage: PageBase
- {
- // The traditional method of calling PageBase
- /**/////<Summary>
- /// Override the OnPreInit () method of the base class and call the common Verification Method
- /// Summary>
- ///<ParamnameParamname="E"> Param>
- Protectedoverrisponidoninit (eventargse)
- {
- Base. Initialize ();
- }
- }
Follow these steps to move the code in PageBase to MasterPage:
MasterPage. cs:
- PublicpartialclassMyMasterPage: System. Web. UI. MasterPage
- {
- ProtectedvoidPage_Load (objectsender, EventArgse)
- {
- If (! IsPostBack)
- {
-
- // Call the Verification Method
- Initialize ();
- }
- }
- }
Modify the code on the Web page:
- PublicpartialclassTestPage: System. Web. UI. Page
- {
- // Call the method in Master following the PageBase Method
- /**////<Summary>
- /// Override the OnPreInit () method of the base class and call the common Verification Method
- /// Summary>
- ///<ParamnameParamname="E"> Param>
- Protectedoverrisponidoninit (eventargse)
- {
- // Obtain the parent page reference
- MyMasterPagemyMasterPage= (MyMasterPage) this. Master;
- // Call the common verification method on the motherboard page
- If (! IsPostBack)
- {
- MyMasterPage. Initialize ();
- }
- }
- }
The preceding sections describe PageBase and MasterPage of ASP. NET.
- Implementation of ASP. NET plug-in
- Overview ASP. NET Applications
- Introduction to ASP. NET 2.0 Data Binding
- ASP. NET prevents Java Script Injection attacks
- ASP. net mvc using T4