ASP. NET component design step by step (5)

Source: Internet
Author: User

Control Lifecycle

 

When an Asp.net page is requested, a page instance is generated and its own logic is started. Finally, the HTML stream is returned to the user. The logic processes the server-side controls and interactions between controls in the page, and the server controls are destroyed before the page ends (depending on the. NET reclaim policy ). In the process of page processing, how have these controls been created, processed, and destroyed? How do these controls interact with other controls and maintain the status between multiple pages? (This should be the basic and important knowledge of the entire control development)

First, let's look at the special controls, that is, the page class, the parent class (or ancestor) of all aspx pages ). This is the main stage for our programmers. The page class inherits from templatecontrol and implements ihttphandler. The ihttphandler interface ensures that the page is scheduled by the Asp.net framework, and can obtain the HTTP data input stream and the ability to output data to the HTTP output stream. The templatecontrol class inherits from the control class. Page implements the inamingcontainer interface, which ensures that it can act as a container for controls on the page (the battle stage for controls)

 

Now let's look at the lifecycle of the control (see how Ms defines the framework system ):

1. instance instantiation

It is instantiated by the constructor of the control. It can also be generated by being instantiated by the parent control.

 

2. initialize Initialization

The Control calls the oninit method by default to trigger the on_init event. The page initializes the control based on the syntax of the ASPX page and the label setting value, and assigns values to the control and its attributes in the Declaration syntax. As a special control, you can generally allow programmers to provide control room initialization operations (assign values to certain attributes) in the oninit event of page ). For controls that contain child controls, controls can access them, but child controls cannot access the parent controls (because the controls are not loaded yet )).

 

3. Begin tracking view State starts to track view status

At the end of the initialization phase, page calls the trackviewstate method of the control (this is a protection method inherited from the control)

 

4. Load view state Loading View

In this case, the page framework automatically restores the viewstate Dictionary (the viewstate data comes from the implicit field in the form), and the control sets its own attributes or internal field variables based on the viewstate value.

 

5. Load PostBack data load and return data

If the control implements the ipostbackdatahandle interface, the interface implemented by the control is called back on the page to process the returned data.

 

6. Load

In this case, all controls in the control tree (tree composed of the page control and its Child Control) have been initialized and restored to the state of the previous cycle (obtained through viewstate ), you can access any other controls.

 

7. Raise change events triggers the modification (Control) event.

Processing the returned data may cause some events of the control to be used as notifications for modifying certain attributes.

 

8. Raise PostBack event triggers the return of data

When a modification event occurs, some events that occur on the client are mapped to the events of the server control to call the event processing routine of the control. These are mostly the customers of control developers-the stage when some programmers reuse controls.

 

9. Pre-generated by prerender

Call the onprerender method of the control to execute any work required before the control is generated. Recursively call this method for child controls.

 

10. Save view State save view status

Controls inherit the control method to save the current control status to viewstate.

11. Render generation

Control outputs HTML data to the HTML stream.

12. Unload uninstall

The page is cleared by implementing the page_onload method, and the unload event of the control is also triggered by default.

 

13. Release dispose

In this case, the control clears the resource occupation.

 

The above discussion is suitable for declaring the created control on the ASPX page. If the control is created in the event handler of the page/control, the control is added to the Control tree to start various stages of execution, after the current page stage is reached, the dynamically created control will work as other controls on the page.

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 265217

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.