Asp.net control development (8) Composite Control (2) behind-the-scenes mechanism of composite control View

Source: Internet
Author: User

Preface:

Third, it is easy to name, because according to the suggestions of netizens. So I changed it to Asp.net control development. Well, let's get started. I talked about the basic concepts of composite controls in the previous section. Today I am going to learn more about how the composite control view tracks the status of sub-controls. How does one save the subcontrol status? How does one load the subcontrol status.

Body:

With these questions, we will go to our learning process. From the above sections, I know that the control class has built-in sub-controls for tracking, saving, and restoring.

. We also know that composite controls inherit from control (control à webcontrol à custom composite controls. View tracking at the beginning

In the state phase, control calls the trackviewstate method of the controls in the controls set in turn to start tracking the status of the child controls. However, if a sub-

The control is added to the controls set after the parent control has enabled the trace state, so the trackviewstate method of the child control is added to the control.

Tree is called at the same time.

When creating a composite control. There is such an override method.

 

Protected override void createdchildcontrols ()

{

//.........

This. Controls. Add (...);

//.........

 

}

 

The add method (controls. Add) in controlcollection is called in this override method ).

Its pseudo code is:

Public writable Ural void add (control child)

{

.........

Int Index = This. _ size;

This. _ SIZE ++;

This. _ owner. addedcontrol (control, index); // _ owner is a control class, and the Internal Control Method addedcontrol is called here;

........

}



 

 

Here, we need to look at the internal implementation of the addedcontrol method. Addedcontrol (Control, int

Index) method to add the child control to the Container Control. In addedcontrol, The viewstate of the child control is processed.

Load the view status at this time. The trackviewstate () method of each sub-control is called.

Addedcontrol pseudocode:

Protected internal virtual voidAddedcontrol(Control, int index)

{ ….

 

Control. loadviewstaterecursive (savedstate); // track the view status and add it to statebag.

}

When (savestateview) saves the view State, control first calls the saveviewstate method. By default, this method is called

The saveview of the viewstate dictionary. And save the returned object. As the first part of the control view status. Next, control the child control in the control tree.

Saveviewstate is called. If the returned sub-control status is not empty. Then control saves the number of the control in two arraylists (in controls

Set) for serialization. The status of these child controls is not empty, while the second arraylist stores The State of the Child controls. Finally, at the end of this stage,

Control returns its three-part view status.

How does the viewstate of the entire Composite Control tree be saved and read?

There are two internal methods in control:
Internal object saveviewstaterecursive ();
Internal void loadrecursive ();
The two methods are called by the saveviewstaterecursive () method of the Control. This method is to call every

The saveviewstaterecursive method of control. Controls is used to save the viewstate of all controls in the control tree. Here, maybe smart friends

I have to ask, since saveviewstaterecursive is a recursive call to save the method, what is the use of the saveviewstate () method we wrote above?
What about it?
We know that control. Controls may have many, and our saveviewstate () only saves the data of the current control, but does not

The structure of the Control tree is recorded. If we use the recursive saveviewstate () method to save data, the structure of the Control tree will be lost, and the load

In fact, the code in the saveviewstaterecursive method is as follows:
[1] obtain the viewstate of the control (call the saveviewstate method)
[2] loop child Control
{
Define two dynamic arrays, one for storing the control index, and one for storing the values returned by recursively calling the saveviewstaterecursive method of the child control.
}
[3] define a list of control names to be PostBack on the current page saved in a triplet // third. The triplet type is actually obtained in the msdn query, it is an object that contains three objects. It has three attributes: first, second, and thrid.
[4] first save the viewstate of this control
[5] Second: Save the sub-Control Index
[6] third: Save the return value of the saveviewstaterecursive method of the recursive subcontrol
[7] returns triplet


In this way, the viewstate of the entire control tree and the structure of the Control tree are saved.

The load method is similar to the Save method, but the sub-control index will be obtained from the savedstate during load to recursively recursive the sub-control's

Loadrecursive () method to ensure that the stored data is correctly transmitted to the Child control.

When (loadviewstate) loads the view State, control executes the return operation that it performs during the Save view State stage. Page at this level

The status that the segment sends to the control is the same as the status that the control last saves in the previous request. Control loads the Saved state by calling the loadviewstate method.

The first part of the state. This method adds the Saved state to the viewstate dictionary in sequence. Next, control accesses the controls set and loads the remaining states.

Sub-control, the remaining status will be composed of arraylist representing the sub-control number and save status. Control adds the status to the subcontrol using these numbers and States.

The sub-controls are saved at the end of the previous request. The status of the Control and its sub-control is restored. If no

Sub-control, the control will save the sub-control status for future use. When the child control is created and added to the Control tree, the control loads the status

Child widget. Note. View state tracking is enabled before the view State loading phase. Therefore, any control property that is automatically restored by using the view State is marked

. In addition, the view State is resaved in the SAVE view State stage. These attributes are reloaded during the view State loading phase during the Back-to-pass, so the view State

The Mechanism is continuously saved in the next step.

The steps explained in each state management phase are recursively executed in the control tree. The view status of some controls indicates the combined view of control layers.

Graph status. Control uses the number of the subcontrol in the controls set to identify the saved view status of the subcontrol. The control does not use the child control type.

Or ID to achieve higher performance. This reduces the size of the view State. However. This means that the view State mechanism is only used when the control tree is re-built after addition and transmission.

The order used is the same as the order in which the last view status of the previous request is saved. It may be maintained by reloading the CONTROLS attribute.

The order in which controls are created in createchildcontrols.

For example:

 

Public override controlcollection controls {

Get {

Ensurechildcontrols ();

Return base. controls;

}

}

 

This Code adds the control tree before other controls are added to the child controls created in createchildcontrols. In

The child Control created in createchildcontrols is re-created in the order of storage when it is returned. In this way, view statuses can be restored in the page box.

Postscript:
1.NET component development series (-) Martial Arts series ----------- Basic Skills
2.NET component development series (-) Martial Arts series -------- Taijiquan development Ajax controls
3..net component development series-Wushu Martial Arts tricks control lifecycle and control event mechanism
4..net control development series event handling mechanism three interfaces two methods
5..net control development (5): a deep understanding of the display principle of controls
6..net control development (6): Exploring the control style
7..net control development (7) Composite Control (1) concept Basics

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.