In the previous series of articles, we introduced the basic concepts and typical applications of view status and control status, from which we can find the importance of view status and control status for custom server control implementation. This article will continue with this topic, focusing on how to implement custom management of view status and control status.
Custom view Status Management
When introducing view status, we once mentioned that for simple attributes such as String and Int ,. NET execution engine will automatically enable the default view status management mechanism to complete the corresponding functions. However, if a Developer Saves a custom data type in ViewState or needs to optimize view State management in a custom mode, the user-defined view State management must be implemented.
You can manage the status of a custom view in two ways. Method 1: implement the IStateManager interface member in the System. Web. UI namespace, including the IsTrackingViewState attribute and the TrackViewState, SaveViewState, and LoadViewState methods. This method is mainly used to manage the view status of custom data types. Method 2: override the three view State management methods of the Control base class: TrackViewState, SaveViewState, and LoadViewState. These methods are the same as the three method names defined by the IStateManager interface. This method is mainly used to optimize the default view State management in a custom way. Its main purpose is to improve efficiency and performance. The shortcut to understanding the above two methods is that you must have a deep understanding of the process of implementing view State management within the. NET Framework. The following two sections describe the internal implementation methods. Each section contains the implementation code, which is actually equivalent to the instance code. The implementation of custom view State management of all server controls does not deviate from the logic expressed by those codes. When the reader has mastered the internal implementation methods, the implementation of custom view State management will be easily solved.
1. Implement custom view Status Management Based on IStateManager Interface
For complex attributes, most of them need to implement custom view State management. The key is to implement the methods and attributes defined in the System. Web. UI. IStateManager interface. The following lists the IStateManager interface definition code.
public interface IStateManager{ bool IsTrackingViewState {get;} void LoadViewState(object state); object SaveViewState(); void TrackViewState();}
As shown in the above Code, the IStateManager interface requires the class to implement the IsTrackingViewState attribute, as well as the LoadViewState, SaveViewState, and TrackViewState methods. The IsTrackingViewState attribute is defined. When implemented by a class, a Boolean value is obtained to indicate whether the server control is tracking its view state changes. If the server control is tracking its view status changes, the value is true; otherwise, the value is false. The SaveViewState method is defined. When implemented by a class, the view State of the server control is changed and saved to the Object. The LoadViewState method is defined. When implemented by a class, the control view state previously saved by the server control is loaded. The parameter state indicates the Object that contains the view state value saved by the control. TrackViewState method definition. When implemented by a class, it instructs the Server Control to track its view state changes.
The ViewState attribute is closely related to the IStateManager interface. The ViewState attribute type is the StateBag class. The StateBag class participates in State management by implementing the methods and attributes defined in the IStateManager interface. The implementation process is as follows.
public sealed class StateBag : IStateManager, IDictionary,ICollection, IEnumerable{
private bool _isTrackingViewState;
private ArrayList _keys;
private ArrayList _values;
private StateItem _item;
bool IStateManager.IsTrackingViewState {
get { return _isTrackingViewState; }
}
void IStateManager.TrackViewState() {
_isTrackingViewState = true;
}
object IStateManager.SaveViewState() {
_keys = new ArrayList();
_values = new ArrayList();
IDictionaryEnumerator myDirctionaryEnumerator = this.GetEnumerator();
while(myDictionaryEnumerator.MoveNext()) {
if(this.Item[(String)myDictionaryEnumerator.Key].IsDirty) {
_keys.Add(myDictionaryEnumerator.Key);
_values.Add(myDictionaryEnumerator.Value);
}
}
if(_keys.Count>0) {
return new Pair(_keys,_values);
}
}
void IStateManager.LoadViewState(object savedState) {
if(savedState is Pair) {
_keys = (ArrayList)tempP.First;
_values = (ArrayList)tempP.Second;
IDictionaryEnumerator myDirctionaryEnumerator = this.GetEnumerator();
while(myDictionaryEnumerator.MoveNext()) {
for(int j=0;j<_keys.Count;j++)
{
if((String)myDictionaryEnumerator.Key == _keys[j].ToString());
{
this.Item[_keys[j].ToString()].Value = (object)_values[j];
}
}
}
}
}
}
Note: The above code is a schematic code, rather than a strict implementation code. It is mainly used to explain the logic process of the StateBag class implementing the IStateManager interface.
Through the above code, we can see that:
(1) In the IsTrackingViewState attribute, set this attribute to read-only and use the private Variable _ isTrackingViewState.
(2) In the TrackViewState method, set the private Variable _ IsTrackingViewState used by the isTrackingViewState attribute to true, which indicates that when a StateItem is added to the StateBag or a StateItem value is modified, the StateBag class automatically marks the StateItem as changed to add the dirty tag.
(3) In the SaveViewState method, every StateItem in the loop StateBag is marked as dirty. Then, the keys and values of the StateItem are added to the two arraylists respectively, and the object is returned.
(4) In the LoadViewState method, the Operation opposite to the SaveViewState method is executed. First, the savedState object is divided into two arraylists that store keys and values, and then the values are loaded into the corresponding StateItem object.
The above is the basic process for implementing the IStateManager interface with the ViewState attribute. All view State management processes must use the above implementation processes. Therefore, understanding the above logic plays an important role in understanding the custom view State management mechanism.