page events: Order and return

Source: Internet
Author: User
By Paul Wilson

L Initialization
2. When the page is submitted, the first method is always the constructor. You can initialize some custom attributes or objects in the constructor, but at this time, the page is not fully initialized, so there are some restrictions. In particular, you need to use the httpcontext object. Currently, you can use querystring, form, cookies, and cache objects. Note: session is not allowed in constructors.

2. The next method to be executed is the addparsedsubobject method. This method adds all independent controls and forms a control set tree on the page, this method is often overwritten by some advanced Page Template solutions to add page content to some special controls in the page template. This method is recursively applied to all page controls and each corresponding sub-control. All controls start the earliest initialization in this method.

2. The next method to be executed in the page class is determinepostbackmode. This method allows you to modify the ispostback value and related events. This method is especially useful if you need to load viewstate from the database, because viewstate can be restored only when ispostback is true. If the return value is null, a non-return request is forcibly executed. If the return value is request. form, a return request is forcibly executed. Unless in special circumstances, it is not recommended to operate this operation, because this will affect other events.

2. The next method to be executed is the oninit method, which is generally the first method actually used. When this method is triggered, all controls in the page definition execute initialization, which means that all the values defined in the page are applied to the corresponding controls. However, viewstate and returned values are not applied to the control. Therefore, no value changed by code or users has been recovered to the control. This method is usually the best place to create and recreate dynamic controls.
L restore and Load
2. In the next method, loadpagestatefrompersistencemedium is executed only when the page is returned. If you modify the savepagestatetopersistencemedium method that will affect viewstate storage because of session or custom storage, this method needs to be overwritten. By default, viewstate is encoded in base64 format and saved in the hidden domain of the page. You can modify viewstate by using the methods mentioned in this article. Note: This method does not actually load viewstate to a page or page control.

2. After viewstate is obtained, the next method loadviewsate will recursively restore viewstate to the page and various page controls or child controls. After this method is executed, each control is restored to the last state, but the data submitted by the user has not been applied to the control, because they are not part of the viewstate. This method is mainly used to restore the value of the control that you dynamically generated in other events. The value is manually saved in viewsate and is invalid now.

2. The next method is processpostdata. This method is executed only when it is returned and cannot be overwritten. This is a private method of the page base class. This method restores the value of the control submitted by the user by matching the control name. This step means that the entire page has been completely restored. The only thing to remember is that all dynamic controls must be created before this method. This method is also used to record subsequent event changes.

2. The next method is the onload method, which is usually the most commonly used method, because this method is the first place to restore all values in the page lifetime. Most Code determines whether to reset the control status based on ispostback. You can also call validate in this method and check the value of isvalid. You can also create a dynamic control in this method, and all the methods of the control will be executed to catch up with the status of the current page, including viewsate, but not the return value.
L event processing
2. The next method is processpostdata, which is actually another call of the previous method. It is still executed only during the callback and cannot be overwritten because it is a private method. If this is the first time you read the running track of the page, you may find this method redundant. But in fact, this method is necessary because the dynamic controls created in onload also need their return values. Any controls created after this will be able to get their viewstate, but will no longer get their return value, and will not trigger any value change event ).

2. The next method, raisechangedevents, is executed only on the return page, and cannot be inherited because it is a private method of the base class. During the lifecycle of the entire page, this event is triggered based on whether the value of the control recorded in the previous processpostdata is different from the submitted value. You may need to call validate or check the value of isvalid. Here, there is no special description that multiple values change the event execution sequence.

2. The next method, raisepostbackevent, is also because the private method of the base class cannot be inherited and is only executed on the return page. Unless autopostback is used, this is where the actual form submission event is executed, especially when buttons or Javascript is used to submit the form. If the verification control has not been manually called and is used, the validate is called. Note that a bug in IE sometimes allows submission but does not trigger any event.

2. The next method is onprerender. Generally, this is the last chance to change the page and its controls before the client displays the page. You can also create a dynamic control in this method, and all the methods will be executed to catch up with the status of the current page including viewsate, but the private method will not be executed, this means that no value is returned and no event is triggered. Due to the bug in IE, this is a good place to catch up with PostBack without events.
L save and display
2. The next method is saveviewstate. Whether it is a return page or not, it is recursively executed to save the viewstate of the page and all its controls. Viewstate basically stores all values different from the original values defined in aspx, whether changed by code or users. Note that the control values are saved based on their locations in the control tree of the page. Therefore, if the dynamic control is subsequently added to an incorrect position, confusion may occur.

2. The next method is savepagestatetopersistencemedium to save the page's viewsate. This method is overwritten along with loadpagestatefrompersistencemediumg to save viewstate to session or other custom data, rather than hiding the domain. This is very helpful for low-bandwidth users. In addition, session is the default setting for mobile devices. The following article describes how to save viewstate using the preceding two methods. Note that there is a bug in requires that the _ viewstate field be submitted, even if it is null.

2. The next method is the render method, which recursively creates and sends the HTML of the corresponding control to the browser. This method is rewritten by some Page Template schemes to add some general page headers and feet without using server controls. They always have some additional things. Note that only HTML can be used for modification here, because the control has been generated here. You can use stringbuilder, stringwriter, and htmltextwriter to capture the corresponding HTML output.

2. The final method is onUnload, which calls the corresponding dispose method. This method provides an opportunity to clear the unmanaged resources used on the page, such as closing the opened file handle and previously opened database connections. Note that this method is executed after the page has been sent to the client, so it only affects the Server Object and will not be displayed in the display track of the page. This is the lifetime of the page, which is run for every request.
L author profile (omitted)

Table 1: Summary of page events

Method return control






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: 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.