2. View Loading
Figure 2
2. The LoadAllState function is called when the view is loaded:
Code
Private void LoadAllState ()
{
Object obj2 = this. LoadPageStateFromPersistenceMedium ();
IDictionary first = null;
Pair second = null;
Pair pair2 = obj2 as Pair;
If (obj2! = Null)
{
First = pair2.First as IDictionary;
Second = pair2.Second as Pair;
}
If (first! = Null)
{
This. _ controlsRequiringPostBack = (ArrayList) first ["_ ControlsRequirePostBackKey _"];
If (this. _ registeredControlsRequiringControlState! = Null)
{
Foreach (Control control in (IEnumerable) this. _ registeredControlsRequiringControlState)
{
Control. LoadControlStateInternal (first [control. UniqueID]);
}
}
}
If (second! = Null)
{
String s = (string) second. First;
Int num = int. Parse (s, NumberFormatInfo. InvariantInfo );
This. _ fPageLayoutChanged = num! = This. GetTypeHashCode ();
If (! This. _ fPageLayoutChanged)
{
Base. LoadViewStateRecursive (second. Second );
}
}
}
Objects obj2 = this. LoadPageStateFromPersistenceMedium () are called in LoadAllState. Objects actually return a Pair object (compare to the SavePageStateToPersistenceMedium parameter in the view)
Go deep into LoadPageStateFromPersistenceMedium:
Protected internal virtual object LoadPageStateFromPersistenceMedium ()
{
PageStatePersister pageStatePersister = this. PageStatePersister;
Try
{
PageStatePersister. Load ();
}
Catch (HttpException exception)
{
If (this. _ pageFlags [8])
{
Return null;
}
Exception. WebEventCode = 0 xbba;
Throw;
}
Return new Pair (pageStatePersister. ControlState, pageStatePersister. ViewState );
}
This function constructs a PageStatePersister object, and then uses pageStatePersister. load () reads the input information on the send-back page and deserializes it to pageStatePersister. controlState and pageStatePersister. viewState. Finally, a Pair object is returned using the restored value.
Return to LoadAllState:
IDictionary first = null;
Pair second = null;
Pair pair2 = obj2 as Pair;
If (obj2! = Null)
{
First = pair2.First as IDictionary;
Second = pair2.Second as Pair;
}
These statements are used to split the Pair object returned by LoadPageStateFromPersistenceMedium, restore First to the IDictionary hash set, which is the status of all controls on the page, and then restore Second to a Pair, this Pair contains the view information of the page Control tree. In fact, the Pair object state constructed by the control state and control view in the SaveAllState is split and restored.
If (first! = Null)
{
This. _ controlsRequiringPostBack = (ArrayList) first ["_ ControlsRequirePostBackKey _"];
If (this. _ registeredControlsRequiringControlState! = Null)
{
Foreach (Control control in (IEnumerable) this. _ registeredControlsRequiringControlState)
{
Control. LoadControlStateInternal (first [control. UniqueID]);
}
}
}
These statements traverse all the controls registered on the page that want to use the control status, in turn, use the unique ID of the control to locate the control status information corresponding to the control in the first hash set (each of the hash sets is a Pair object, if you are not clear, please refer to the SaveAllState Logic saved in the view) and call LoadControlStateInternal
Internal void LoadControlStateInternal (object savedStateObj)
{
If (! This. flags [0x100000])
{
This. flags. Set (0x100000 );
Pair pair = (Pair) savedStateObj;
If (pair! = Null)
{
Page page = this. Page;
If (page = null) | page. ShouldLoadControlState (this ))
{
If (pair. First! = Null)
{
This. LoadControlState (pair. First );
}
If (this. _ adapter! = Null) & (pair. Second! = Null ))
{
This. _ adapter. LoadAdapterControlState (pair. Second );
}
}
}
}
}
In LoadControlStateInternal, the incoming parameters are first converted to Pair objects, and then pair. first, call LoadControlState to read the control state of the current control (like SaveControlState, this is a virtual empty function that needs to implement its own logic). this. _ adapter! = Null if the adapter is not empty, pair. second calls LoadAdapterControlState to load the control status of the current control from the adapter perspective. LoadAdapterControlState is a virtual empty function. If an adapter is specified for the control, the status loading logic of the adapter control should also be implemented, it can be seen that LoadControlStateInternal is the inverse process of SaveControlStateInternal-loading control status.
Return to LoadAllState
If (second! = Null)
{
String s = (string) second. First;
Int num = int. Parse (s, NumberFormatInfo. InvariantInfo );
This. _ fPageLayoutChanged = num! = This. GetTypeHashCode ();
If (! This. _ fPageLayoutChanged)
{
Base. LoadViewStateRecursive (second. Second );
}
}
As mentioned above, the Second of the Pair returned by LoadPageStateFromPersistenceMedium is the control view, so the core of the above statements is base. loadViewStateRecursive (second. second) for pair. second. second recursively reads the view information of the Control tree. See LoadViewStateRecursive:
Internal void LoadViewStateRecursive (object savedState)
{
If (savedState! = Null )&&! This. flags [4])
{
If (this. Page! = Null) & this. Page. IsPostBack)
{
Object first = null;
Object state = null;
ArrayList childState = null;
Pair pair = savedState as Pair;
If (pair! = Null)
{
First = pair. First;
ChildState = (ArrayList) pair. Second;
}
Else
{
Triplet triplet = (Triplet) savedState;
First = triplet. First;
State = triplet. Second;
ChildState = (ArrayList) triplet. Third;
}
Try
{
If (state! = Null) & (this. _ adapter! = Null ))
{
This. _ adapter. LoadAdapterViewState (state );
}
If (first! = Null)
{
This. LoadViewState (first );
}
If (childState! = Null)
{
If (this. LoadViewStateByID)
{
This. LoadChildViewStateByID (childState );
}
Else
{
This. LoadChildViewStateByIndex (childState );
}
}
}
Catch (InvalidCastException)
{
Throw new HttpException (SR. GetString ("Controls_Cant_Change_Between_Posts "));
}
Catch (IndexOutOfRangeException)
{
Throw new HttpException (SR. GetString ("Controls_Cant_Change_Between_Posts "));
}
}
This. _ controlState = ControlState. ViewStateLoaded;
}
}
First
Object first = null;
Object state = null;
ArrayList childState = null;
Pair pair = savedState as Pair;
If (pair! = Null)
{
First = pair. First;
ChildState = (ArrayList) pair. Second;
}
Else
{
Triplet triplet = (Triplet) savedState;
First = triplet. First;
State = triplet. Second;
ChildState = (ArrayList) triplet. Third;
}
This is based on the input LoadViewStateRecursive parameter value savedState to determine whether it is a Pair or a Triplet, and then restores it to the first control view and state adapter view according to the corresponding situation, hash set of the childState subcontrol view (compared with the corresponding inverse process in SaveViewStateRecursive)
Next
If (state! = Null) & (this. _ adapter! = Null ))
{
This. _ adapter. LoadAdapterViewState (state );
}
If (first! = Null)
{
This. LoadViewState (first );
}
If (state! = Null) & (this. _ adapter! = Null) indicates that if the restored adapter view information is not empty and the control adapter is not empty, this is used. _ adapter. loadAdapterViewState (state) reads the control's view information from the adapter perspective (LoadAdapterViewState is implemented by the virtual empty function logic), and then this. loadViewState (first) loads the first view information to this control through LoadViewState:
Protected virtual void LoadViewState (object savedState)
{
If (savedState! = Null)
{
This. ViewState. LoadViewState (savedState );
Object obj2 = this. ViewState ["Visible"];
If (obj2! = Null)
{
If (! (Bool) obj2 ))
{
This. flags. Set (0x10 );
}
Else
{
This. flags. Clear (0x10 );
}
This. flags. Set (0x20 );
}
}
}
It can be seen that the core of LoadViewState is this. ViewState. LoadViewState (savedState) to load the ViewState set of this control. Of course, according to the attempt to save the SaveViewState, return an ArrayList. The savedState input here should also be an ArrayList
Return to LoadViewStateRecursive
If (childState! = Null)
{
If (this. LoadViewStateByID)
{
This. LoadChildViewStateByID (childState );
}
Else
{
This. LoadChildViewStateByIndex (childState );
}
}
These statements load view information based on the subcontrol ID or the sequence of the subcontrol in the control tree.
Internal void LoadChildViewStateByID (ArrayList childState)
{
Int count = childState. Count;
For (int I = 0; I <count; I + = 2)
{
String id = (string) childState [I];
Object savedState = childState [I + 1];
Control control = this. FindControl (id );
If (control! = Null)
{
Control. LoadViewStateRecursive (savedState );
}
Else
{
This. EnsureOccasionalFields ();
If (this. _ occasionalFields. ControlsViewState = null)
{
This. _ occasionalFields. ControlsViewState = new Hashtable ();
}
This. _ occasionalFields. ControlsViewState [id] = savedState;
}
}
}
Internal void LoadChildViewStateByIndex (ArrayList childState)
{
ControlCollection controls = this. Controls;
Int count = controls. Count;
Int num2 = childState. Count;
For (int I = 0; I <num2; I + = 2)
{
Int num4 = (int) childState [I];
Object savedState = childState [I + 1];
If (num4 <count)
{
Controls [num4]. LoadViewStateRecursive (savedState );
}
Else
{
This. EnsureOccasionalFields ();
If (this. _ occasionalFields. ControlsViewState = null)
{
This. _ occasionalFields. ControlsViewState = new Hashtable ();
}
This. _ occasionalFields. ControlsViewState [num4] = savedState;
}
}
}
As mentioned above, the views and controls of the controls in the array list are one-to-one. Therefore, whether LoadChildViewStateByID or LoadChildViewStateByIndex is used to read view information from the array, then, find the corresponding control based on the read data and call LoadViewStateRecursive to call its view information savedState (which is a Pair or Triplet) to recursively load the view of the subcontrol.
In this way, LoadControlStateInternal is called cyclically in LoadAllState and LoadViewStateRecursive is called recursively. The view status and view information of all controls on the page are loaded.
It can be seen that the loading of a view is the inverse process of view storage. You will find that each step of the loading process can correspond to the corresponding steps during the storage process, therefore, we need to understand the two processes through comparison.