Custom state management for ASP.NET2.0 server control

  • 2020-05-05 11:07:17
  • OfStack

In the previous series of articles, we introduced the basic concepts and typical applications of view state and control state, and we saw how important view state and control state are to custom server control implementations. This article continues this theme, focusing on ways to implement custom management of view state and control state.

custom view state management

When we talked about view state, we mentioned that for simple properties, such as String, Int, etc., the.NET execution engine automatically enables the default view state management mechanism to complete the function. However, if the developer is storing custom data types in ViewState or needs to implement custom ways to optimize view state management, then custom view state management must be implemented.

There are two ways to implement custom view state management. Method 1: implement the IStateManager interface member in the System.Web.UI namespace, which includes the IsTrackingViewState attribute and the TrackViewState, SaveViewState, and LoadViewState methods. This approach is primarily a case of view state management for custom data types. Method 2: override the three view state management methods of the Control base class: TrackViewState, SaveViewState, and LoadViewState. These methods are consistent with the three method names defined by the IStateManager interface. This approach is primarily used in cases where the default view state management is optimized in a custom manner with the primary goal of improving efficiency and performance. The easiest way to master both approaches is to have a deep understanding of how view state management is implemented within the NET framework. The following two sections are an introduction to the internal implementation methods. There is implementation code in each section, which is the equivalent of instance code. The implementation of custom view state management for all server controls does not deviate from the logic expressed by that code. Once the reader has a real grasp of those internal implementations, the implementation of custom view state management will be readily resolved.

1. Implement custom view state management
based on IStateManager interface
For complex properties, most need to implement a custom view state management, the key is to realize the System. Web. UI. IStateManager interface defined in the methods and properties. The IStateManager interface definition code is listed below.

public interface IStateManager{ bool IsTrackingViewState {get;} void LoadViewState(object state); object SaveViewState(); void TrackViewState();}
As shown in the code above, the IStateManager interface requires the class to implement the IsTrackingViewState properties, as well as the LoadViewState, SaveViewState, and TrackViewState methods. The IsTrackingViewState property defines that, when implemented by a class, gets a Boolean value that indicates whether the server control is tracking its view state changes. true if the server control is tracking its view state changes; Otherwise, false. The SaveViewState method is defined to save the view state changes of the server control to Object when implemented by the class. The LoadViewState method defines that, when implemented by a class, the control view state previously saved by the server control is loaded, where the parameter state represents Object containing the view state value saved by the control. The TrackViewState method is defined to instruct the server control to track its view state changes when implemented by a class.

There is a close relationship between the ViewState attribute and the IStateManager interface. The type of ViewState attribute is the StateBag class, which 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 & # 123;
get & # 123; return _isTrackingViewState; The & # 125;
The & # 125;
void IStateManager. TrackViewState () & # 123;
_isTrackingViewState = true;
The & # 125;
object IStateManager. SaveViewState () & # 123;
_keys = new ArrayList ();
_values = new ArrayList ();
IDictionaryEnumerator myDirctionaryEnumerator = this. GetEnumerator ();
while (myDictionaryEnumerator MoveNext ()) & # 123;
if (this Item [r]. (String) myDictionaryEnumerator. Key IsDirty) & # 123;
_keys. Add (myDictionaryEnumerator. Key);
_values. Add (myDictionaryEnumerator. Value);
The & # 125;
The & # 125;
if (_keys Count> 0) {
return new Pair (_keys _values);
The & # 125;
The & # 125;
void IStateManager. LoadViewState (object savedState) & # 123;
if (savedState is Pair) & # 123;
_keys = (ArrayList) tempP. First;
_values = (ArrayList) tempP. Second;
IDictionaryEnumerator myDirctionaryEnumerator = this. GetEnumerator ();
while (myDictionaryEnumerator MoveNext ()) & # 123;
for (int j = 0; j< _keys.Count;j++)
The & # 123;
if ((String) myDictionaryEnumerator Key = = _keys [j] ToString ());
The & # 123;
this. Item [_keys [j] ToString ()]. Value = (object) _values [j];
The & # 125;
The & # 125;
The & # 125;
The & # 125;
The & # 125;
}

Please note that the above code is schematic code, not strictly implementation code. It is listed here primarily to illustrate the logical process by which the StateBag class implements the IStateManager interface.

From the above code, we can see:

(1) in the IsTrackingViewState property, set the property to read-only and use the private variable _isTrackingViewState.

(2) in the TrackViewState method, the private variable _isTrackingViewState used by the IsTrackingViewState attribute is set to true, which indicates that when an StateItem is added to StateBag or an StateItem value is modified, the StateBag class will automatically mark the StateItem as modified, that is, add the dirty tag.

(3) in the SaveViewState method, loop each StateItem in StateBag, if the StateItem is marked as dirty, then add its key and value to each ArrayList and return the object.

(4) in the LoadViewState method, the operation opposite to the SaveViewState method is performed. The savedState object is first decomposed into two ArrayList objects that hold keys and values, and then the values are loaded into the corresponding StateItem object.

That's the basic process for the ViewState property to implement the IStateManager interface. All view state management processes use the above implementation process, so understanding the above logic is critical to gaining insight into custom view state management mechanisms.

 


Related articles: