ViewState Analysis (Translation and Notes)
Original link: ViewState: All You Wanted to Know Author: Paul Wilson translation: wood Wildfox
What is you not?
1. ViewState is not the value of the control used to recover the return. This is automatically complete by matching the variable name of the control in the Form. This is valid only if the control before loading the Load event is loaded. 2. ViewState does not automatically recreate any controls created by code dynamics. 3. Not used to save user information. Just save the status of the control on this page without passing between the page.
What is viewState?
ViewState is used to track and save status information for the control. Otherwise, this information may be lost because these values are not in the HTML of the Page without the form of Form. The control properties changed in the code are saved, bind to any data to the control through the code, and any changes that are triggered by the user. ViewState also provides a state package (StateBag), which is a special collection or dictionary that can be saved, and any object or value is restored by a key.
ViewState's format
Save __viewstate hide fields in the form. Is Base64 encoded, not encryption! However, it is also possible (setting EnableViewStateMac to use Machine Key to use Machine Key) Encryption: Set MachineKey verification, but this must be set in the machine level, need more resources, so it is not recommended.
Listing 1: ViewState Machine Hash Disabled
Machine.config or Web.config:
Page Level Directive: <% @ Page EnableViewStateMac = 'false'%>
Page Level Script Code: Page.enableViewStateMac = False;
Listing 2: ViewState Encryption is enabled
Machine.config:
WHERE The ValidationKey Must Be The Same Across A WEB-FARM SETUP
Also Requires The EnableViewStatemac Property Setting To Be True
Before rendering, ViewState is saved in the page.savepagestatetopistencemedium method, and is restored in the page.loadpagestateFromPersistanceMedium method. Both methods can be easily overwritten, thereby implementing the save viewState to the Session. This is suitable for a small bandwidth, such as the mobile device uses session. The code is as follows:
Listing 3: ViewState Saved in Session State
Protected Override Object LoadPagestateFromPersistencemedium ()
{
Return session ["ViewState"];
}
Protected Override Void SavepageStatetopistenceMedium (Object ViewState)
{
Session ["ViewState"] = viewState;
// bug Requires Hidden Field __ViewState
Registerhiddenfield ("__viewstate", "");
}
If you want to maintain ViewState through a database or other persistence devices, you need to serialize and deserialize the specific LosFormatter class. (Serialize, DeSerialize)
Listing 4: ViewState Saved In Custom Store
Protected Override Object LoadPagestateFromPersistencemedium ()
{
Losformatter Format = new losformatter ();
Return format.deSerialize (YourDataStore ");
}
Protected Override Void SavepageStatetopistenceMedium (Object ViewState)
{
Losformatter Format = new losformatter ();
StringWriter Writer = new stringwriter ();
Format.Serialize (Writer, ViewState);
YourDataStore ["ViewState"] = Writer.toString ();
}
Finally, let's take a look at what is the internal format of ViewState.
The ViewState of each control is saved in a triple group (Triplet, System.Web.ui.trillet).
Its first object is:
A pair (System.Web.ui.Pair)
or
Array or Pairs, of ArrayLists of Related Name-Values.
SECOND object:
ArrayList of the index of the control in the control tree
Third object:
Similar three-yuan group of child controls
Listing 5: ViewState Decode / Parse EXAMPLE
Encoded ViewState:
DDWXMJM0NTY3ODKWO3Q8CDXSPHBYCEE7CHJWQJTWCNBDOZ47BDX2YWXBO3ZHBEI7DMFSQZS PJTSPGK8
MD47ATWYPJTPPDM O2K8NT47PJTSPHQ8CDXSPHBYCEE7CHJWQJS O2W8DMFSQTTT2YWXCOZ4 OZS O3Q8
CDXSPHBYCEE7CHJWQJS O2W8DMFSQTTTT2YWXCOZ4 OZS O3Q8cDxsphbycee7chjwqjs O2W8DMFSQTT2
YWXCOZ4 OZS O3Q8CDXSPHBYCEE7CHJWQJS O2W8DMFSQTT2YWXCOZ4 OZS OZ4 OZ4 =
Decoded ViewState:
T
; l >;
L ; i <2>; i <3>; i <5>;>; l <
T
; l ; l > ;; T ; l T ; l > ;;>; >>;> Resolved ViewState: T <1234567890; page level ternary group is a special case T ; triplet-first: pair-first: arraylist L " L ; Triplet-Second: ArrayList: INDICES i <2>; of the i <3>; Children I <5>; Controls " L ; triplet-third: arraylist: Triplets L CHildren Controls " T ; Each Sub-Triplet Follows Same Pattern l >>>> " More Levels Possible if Sub-Children " T ; Each Sub-Triplet Follows Same Pattern l >>>> " More Levels Possible if Sub-Children " T ; Each Sub-Triplet Follows Same Pattern l >>>> " More Levels Possible if Sub-Children " > >; Closing of Special Page-Level Triplet > Listing 6: ViewState Decode / Parse Code