Asp.net| Server | Control in the previous series, we have described the basic concepts and typical applications of view state and control state, from which you can discover the importance of view state and control state for custom server control implementations. This article will continue with this topic, highlighting ways to implement view state and control state customization management.
Customizing view state Management
When we introduce view state, we have mentioned that for simple properties, such as String, int, and so on. The net execution engine automatically enables the default view state management mechanism to complete the appropriate functionality. However, custom view state management must be implemented if the developer saves a custom data type in ViewState, or if you need to implement a custom way to optimize view state management.
There are two ways to implement a custom view state management. Method One: Implement IStateManager interface members in the System.Web.UI namespace, including isTrackingViewState properties and TrackViewState, SaveViewState and LoadViewState methods. This approach is primarily for view state management of custom data types. Method Two: Override 3 view state management methods for the control base class: TrackViewState, SaveViewState, and LoadViewState. These methods are consistent with the 3 method names defined by the IStateManager interface. This approach is primarily used to optimize the default view state management through customization, with the main purpose of improving efficiency and performance. The shortest way to master both of these approaches is to have a deep understanding. NET Framework to implement the process of view state management. The following two sections are an introduction to internal implementation methods. The implementation code in each section is actually equivalent to the instance code. The implementation of Custom view state management for all server controls does not deviate from the logic expressed by those codes. When the reader really grasps those internal implementation methods, then the implementation of the custom view state management is solved.
1. Implement custom view state management based on IStateManager interface
For complex properties, most need to implement custom view state management, and the key is to implement the methods and properties defined in the System.Web.UI.IStateManager interface. 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 property, as well as the LoadViewState, SaveViewState, and TrackViewState methods. The isTrackingViewState property definition, when implemented by a class, gets a Boolean value indicating 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 definition, when implemented by a class, saves the server control's view state changes to object. The LoadViewState method definition, when implemented by a class, loads the control view state previously saved by the server control, where the parameter state represents an object that contains the view status value saved by the control. The TrackViewState method definition, when implemented by a class, instructs the server control to track its view state changes.
There is a close connection between the ViewState property and the IStateManager interface. The type of the ViewState property is the StateBag class, and the StateBag class participates in state management by implementing the methods and properties 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];
}
}
}
}
}
}
Please note: The above code is schematic code, not strictly the implementation code. In this list, it is mainly used to illustrate the logical process of implementing the IStateManager interface of the StateBag class.
Through the code above, 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, set the private variable _istrackingviewstate used by the isTrackingViewState property to True, which indicates that when a stateitem is added to the StateBag, Or when a Stateitem value is modified, the StateBag class automatically marks the Stateitem as modified to add the dirty tag.
(3) in the SaveViewState method, each Stateitem in the loop statebag, if the Stateitem is marked as dirty, adds its keys and values to two ArrayList, and returns the object.
(4) in the LoadViewState method, an operation contrary to the SaveViewState method is performed. The Savedstate object is first decomposed into two ArrayList that hold keys and values, and then the values are loaded into the corresponding Stateitem object.
The above is the basic process of implementing the IStateManager interface for the ViewState attribute. All view state management processes use the above implementation process, so understanding the above logic plays an important role in the deep mastery of the custom view state management mechanism.
2. Implement custom view state management based on the control base class
If the developer needs to optimize the default view state management mechanism to improve the efficiency and performance of the control, you must understand the default view state management mechanism in the controls base class. By mastering this management mechanism, you can emulate its process to implement custom view state management.
Implementing custom view state management based on the control base class requires developers to implement 3 methods: LoadViewState, SaveViewState, and TrackViewState. They have the same name as the IStateManager interface member method described in the previous section, and are essentially the same in the methodological sense. There is no more explanation for these 3 methods.
The default view state management mechanism in the control base class defines a statebag type of ViewState property and delegates the task of view state management to it. See the implementation logic for the default state management for the control base class below.
Private StateBag _viewstate;
Protected virtual StateBag viewstate{
get {
if (_viewstate!= null)
{
return _viewstate;
}
_viewstate = new StateBag (viewstateignorescase);
if (istrackingviewstate)
_viewstate.trackviewstate ();
return _viewstate;
}
}
protected virtual void TrackViewState () {
if (_viewstate!= null) {
_viewstate.trackviewstate ();
}
return null;
}
Protected virtual Object SaveViewState () {
if (_viewstate!= null) {
_viewstate.saveviewstate ();
}
return null;
}
protected virtual void LoadViewState (object savedstate) {
if (savedstate!= null) {
Viewstate.loadviewstate (savedstate);
}
}
From the above code you can see that the ViewState property is a statebag type and returns _viewstate when _viewstate is not NULL, and initializes a variable of type _viewstate when StateBag is null. ViewState and determines whether the control is tracking its view state changes, and if the server control is tracking its view state changes, call the TrackViewState method to start the status trace, and then return to _viewstate. In addition, in the TrackViewState, SaveViewState, LoadViewState methods, the method of view state management in the StateBag class is used.
In the default view state management process of the control base class, because the ViewState property is defined as a statebag type, the logic of view state management is necessarily implemented using the StateBag class above. If the control base class implements the methods and attributes in the IStateManager interface, the implementation process must be similar to the IStateManager interface of the StateBag class, which will inevitably result in duplication. NET Framework changes the access nature of the IStateManager interface. In addition, in the process of customizing view state management, there may be a conflict between the StateBag type and the view state management mechanism of the control base class, which can create confusion. In the present way, it has many advantages, whether from flexibility, inheritance and reusability, and from the custom of programmers.
customizing control state Management
View state is not quite the same as control state in data management. ASP.net 2.0 supports custom view state management while supporting the default view state management mechanism for simple properties. However, for control state management, there is no default control state management mechanism. The developer must implement the custom control state management process. This section describes the implementation of custom control state management, which is important for implementing objects based on control state functionality.
In fact, in the previous series of articles describing control state, the reader has contacted the contents of the control state implementation, including the implementation of custom control state management. The implementation of this process is very similar to the custom view state management based on the control base class, both of which need to override the methods in the base class. Implementing a custom control state management requires overriding the SaveControlState and loadcontrolstate of the controls base class. A simple example code is listed below.
public class Sample:control {
private int currentindex = 0;
protected override void OnInit (EventArgs e) {
Page.registerrequirescontrolstate (this);
Base. OnInit (e);
}
protected override Object SaveControlState () {
Return currentindex!= 0? (object) Currentindex:null;
}
protected override void LoadControlState (object state) {
if (state!= null) {currentindex = (int) state;}
}
}
Implementing custom control state management is divided into 3 key steps:
(1) Call the Registerrequirescontrolstate method. This method is used to register the custom control as a control with persistent control state.
(2) rewrite the SaveControlState method. This method is used to save any server control state changes that occurred since the page was sent back to the server.
(3) rewrite the LoadControlState method. This method is used to restore control state information from the previous page saved by the SaveControlState method.
Summary
This chapter mainly describes the content of custom view state and control state management. They are significant for implementing custom server controls. It is suggested that readers should understand the theoretical knowledge first, and then try the development work, so that they can get better learning effect.