ASP.NET component design step by step (5)

xiaoxiao2021-03-06  26

Control survival cycle

When an ASP.NET page is requested, a PAGE instance is generated, starting its own logic, eventually returns to the HTML flow to the user. The constituent logic is the server-side controls existing in Page, and the interaction between the controls, and the server controls are destroyed (depending on the reclaim policy of the .NET) before the end of the page. Then these controls have experienced creation, processing, and destruction in the short page processing. How is it organized? How do these controls interact with other controls, how do you keep status between multiple pages? (This should be the basics of the development of the entire control.

First, let's take a look at the special control, that is, the parent class (or ancestor) of all ASPX pages. Because this is the main stage of our programmer. The Page class inherits from TemplateControl and implements IHTTPHANDLER. The IHTTPHANDLER interface is to ensure that the page is scheduled by the ASP.NET framework, and the data input stream of the HTTP protocol can be obtained, and the ability to obtain data to the HTTP output stream; the TemplateControl class is inherited from the Control class. Page implements the InamingContainer interface, which guarantees that he can act as a container of the controls in the page (the battle stage of the controls)

Ok, now look at the life cycle of the control (see how the MS defines this framework system):

1, instance instantiation

The controlled constructor is instantiated. It can also be generated by instantiating the parent control.

2, Initialize initialization

The control will call the ONIT method by default, thus causing an ON_INIT event. Page initializes the control according to the syntax and tag setting value of the ASPX page, assigns the controls in the declaration syntax and its attributes. As a special control, the programmer can be allowed to provide an initialization operation (for some attribute assignments) in the onInit event in Page. For sub-controls included in the control, the control can access them, but the child control is not accessible to the parent control (because the control has not been loaded).

3, Begin Tracking View State Start Tracking View Status

At the end of the initial phase, Page will call the TRACKVIEWSTATE method of the control (this is a protection method inheriting from the Control)

4, Load View State Load View

At this point, the page framework automatically restores the ViewState dictionary (ViewState data from the implied field in the form of form), the control will set your own attribute or internal field variable according to the viewState value.

5, load postback data loads backup data

If the control implements the iPostBackDataHandle interface, then the page is called back to the interface to implement the interface to participate in the processing of the return data.

6, LOAD load

At this point, all controls have been initialized all controls, and restore to the status of the previous cycle (which is obtained through ViewState), which can access other controls.

7. Raise Change Events triggers a modification (control) event

Processing the biochable data, which may cause some events of the control as a notification that is modified to some attributes.

8, Raise Postback Event triggers biochemical data

When modifying events, the events of events that map some of the events occurred to the server control occurred to call the control routine of the control. Most of them are customer clients - Other programmers reuse the stage when reusing controls.

9, prerender precompensation

Perform any work you need before generating the control by calling the onprender method of the control. This method of recursive recursive sub-controls. 10. Save View State Save View Status

Controls inherit the Control method to save the current control status to ViewState

11, Render generation

The control output HTML data is in the HTML stream.

12, unload uninstall

The page performs clearing work by implementing the Page_OnLoad method, also trigger the unload event of the control by default

13, Dispose release

At this point, the control performs the method of clearing the occupied resource.

The above discussion is suitable for the control created in the ASPX page. If it is created in the event handler of the page / control, the control is created in the control tree, starting to perform each stage until the current phase of the page, then Dynamic created controls will work as other controls in the page.

转载请注明原文地址:https://www.9cbs.com/read-65436.html

New Post(0)