ViewState mechanism from simple to deep 2

Source: Internet
Author: User
Archive. cnblogs. the coma11221_2.2.2pair class and ViewState storage Page and the ViewState and ControlState of all controls are stored in the Pair class instance. It is important to understand how the Pair class and ViewState are stored in the Pair class. The Pair-defined System. Web. UI is defined as follows: publicsealedcia

Http://archive.cnblogs.com/a/1122213/ 2.2.2 Pair class and ViewState storage Page and all controls ViewState, ControlState are stored in the Pair class instance, understanding Pair class and ViewState how to store in Pair class is very important. The Pair-defined System. Web. UI is defined as follows: public sealed linoleic

Http://archive.cnblogs.com/a/1122213/


2.2.2 Pair class and ViewState Storage

The ViewState and ControlState of pages and all controls are stored in the Pair class instance. It is important to understand how the Pair class and ViewState are stored in the Pair class. The Pair System. Web. UI is defined as follows:

Public sealed class Pair

{

Public object First;

Public object Second;

Public Pair ();

Public Pair (object x, object y );

}

We can see that the Pair class is used as the basic structure to store two related objects. Pair is a tool class that can easily form a tree data structure. Deserializing _ VIEWSTATE is a Pair object. This object stores the parent-child relationship between controls and ViewState information. The Key/Value Pair in ViewState is converted into an ArrayList object and saved in the Pair object. Let's take a look at how it is stored.


It is a schema representation of Pair. The left side indicates the First object and the right side indicates the Second object. Based on this scheme, the Pair object is used to store the ViewState structure, there is no specific expansion of ControlState. The savedState (Pair) uses recursive storage methods to store ViewState and relationships of all controls, and stores ViewState data in the First part of the Pair object, store the sub-control information in Second.


2.2.3 handling in Control

The Page class inherits from the Control class. When the ViewState mechanism is implemented, the Page class mainly involves serialization and deserialization, the storage and loading functions of ViewState are inherited by the Page class in the Control class, and controls are also inherited from the Control class, the following describes the attributes and methods related to the ViewState mechanism in the Control class.
1) ViewState attributes
ViewState is an attribute of the Page and Control. This attribute is not defined in the Page or Control. It is defined in the System. Web. UI. Control class. Its statement is as follows:

Protected virtual StateBag ViewState {get}

All server-side controls, user-defined controls, and Page classes inherit from the Control class, so they all have a protected ViewState attribute. This ViewState attribute is very important in the ViewState mechanism. It is used to store some data that the control needs to remember. There are two main types of data: one is the properties of controls that need to be remembered. Another type is the data we want to use the ViewState mechanism to remember. For example, we write the following code in a Web form:

Protected void Page_Load (object sender, EventArgs e)

{

If (! This. IsPostBack)

{

This. ViewState ["Test"] = 0;

}

Else

{

This. ViewState ["Test"] = int. Parse (this. ViewState ["Test"]. ToString () + 1;

}

    }
Because the Web forms we create all inherit from the Page class, Can we access the protected ViewState attribute of the Page class in the Web form. We can use the ViewState mechanism through the ViewState attribute to help us remember some information.
The ViewState attribute is mainly used to remember the property values of some controls. What is the relationship between the ViewState attribute and other properties of the control so that the ViewState attribute can be used to remember them? The following uses the Text attribute of the Button class as an example to illustrate the relationship between the Text attribute and the ViewState attribute. The definition of Button. Text is as follows:

Public string Text

{

Get

{

String str = (string) this. ViewState ["Text"];

If (str! = Null)

{

Return str;

}

Return string. Empty;

}

Set

{

This. ViewState ["Text"] = value;

}

}

Through the code above, we can see that the Value of the Button. Text attribute is actually associated with the Value whose Key is "Text" in ViewState. Of course, other properties of the Button class are similar, but they only correspond to different keys in ViewState. In this way, as long as the value of ViewState can be remembered, the attributes of the Button class can be remembered. The method for memorizing ViewState is in Page. in SaveAllState, an object is generated based on the ViewState attribute of all controls (this is a special object that stores the ViewState attribute of all controls in a tree State), and the object is serialized as a string, send to the client. In the next request, send the string to the server on the Page. this string is deserialized into an object in LoadAllState. The ViewState attribute of each control stored in this object is loaded to the ViewState attribute of each control. When we access the properties of the control, we can remember the properties of the control. The previously set properties of the control are not lost.
2) LoadViewStateByID attribute

The LoadViewStateByID statement is as follows:

Protected bool LoadViewStateByID {get}

LoadViewStateByID gets a value indicating whether the control loads its view status by ID. By default, the view status is loaded by indexing. The indexing mode depends on the position of the child control in the Controls collection of the parent control. The ID method looks up controls based on the ID, which is less efficient. However, in some cases, you must use this method, for example, when creating a child control.

3) EnableViewState attribute
EnableViewState is a Boolean attribute to determine whether the ViewState mechanism of the current control is available. The statement is as follows:

Public virtual bool EnableViewState {get, set}

4)     LoadViewStateRecursive
The LoadViewStateRecursive statement is as follows:
internalvoid LoadViewStateRecursive(object savedState)
In Page. LoadAllState, Control. LoadViewStateRecursive is called. The input savedState parameter is a Pair-type object. Pair. Fisrt stores the ViewState of the current Control and Pair. Second stores the ViewState of the child Control. When EnableViewState is true, the ViewState of the current Control is loaded by calling Control. LoadViewState (savedState. First. Then, load the ViewState of the child control according to the LoadViewStateByID attribute. If the LoadViewStateByID attribute is true, call Control. LoadChildViewStateByID (savedState. Second). Otherwise, call
Control. LoadChildViewStateByIndex (savedState. Second ). SavedState. Second is an ArrayList object.

The declaration of Control. LoadViewState is as follows:

protected virtual void LoadViewState(object savedState)
In Control. LoadViewState, The ViewState of the current Control is not loaded, but is implemented through ViewState. LoadViewState (savedState). The LoadViewState method of StateBag is described below.
The declaration of LoadChildViewStateByID is as follows:
internal void LoadChildViewStateByID(ArrayList childState)
The childState parameter is of the ArrayList type. childState stores information about all the child controls of the current control. The format is the ID of a control first, followed by the Pair object of the control. Loop childState to obtain the Control ID in the loop. Find the control by ID and call the LoadViewStateRecursive method of the control. The sample code is as follows:

For (int I = 0; I <count; I + = 2)

{

String id = (string) childState [I];

Object savedState = childState [I + 1];

Control control = this. FindControl (id );

Control. LoadViewStateRecursive (savedState );

}

The declaration of LoadChildViewStateByIndex is as follows:

Internal void LoadChildViewStateByIndex (ArrayList childState)

The storage format of childState is a control index, followed by the Pair object of the control. Access this control based on the index and call its LoadViewStateRecursive method. The sample code is as follows:

For (int I = 0; I <num2; I + = 2)

{

Int num4 = (int) childState [I];

Object savedState = childState [I + 1];

Controls [num4]. LoadViewStateRecursive (savedState );

}

5)     LoadViewState

In LoadViewState, call the LoadViewState method of ViewState to load ViewState. The Code is as follows:

This. ViewState. LoadViewState (savedState );

6)     SaveViewStateRecursive
The SaveViewStateRecursive statement is as follows:

Internal object SaveViewStateRecursive ()

If EnableViewState is true, the ArrayList object x that contains the ViewState information of the current Control is returned by calling Control. SaveViewState. Then the child control is recursively processed to obtain an ArrayList Object z. The format is ID (String), savedState (Pair), or Index (int) savedState (Pair ). Finally, create a Pair object Pair (x, z ). The sample code is as follows:

Object x = this. SaveViewState ();

ArrayList z = null;

ControlCollection controls = this. _ occasionalFields. Controls;

Int count = controls. Count;

Bool loadViewStateByID = this. LoadViewStateByID;

For (int I = 0; I <count; I ++)

{

Control control = controls [I];

Object obj4 = control. SaveViewStateRecursive ();

If (loadViewStateByID)

Z. Add (control. ID );

Else

Z. Add (I );

Z. Add (obj4 );

}

Return new Pair (x, z );

7)     SaveViewState

In SaveViewState, call the SaveViewState method of ViewState to save ViewState. The Code is as follows:

Return this. ViewState. SaveViewState ();

8)     TrackViewState

In Control. InitRecursive, Control. TrackViewState is called recursively. Therefore, the TrackViewState of each Control is called during initialization. In Control. TrackViewState, call the TrackViewState method of ViewState. The Code is as follows:

This. ViewState. TrackViewState ();

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.