asp.net2.0 the custom state management of server controls _ practical tips

Source: Internet
Author: User

In the previous series, we introduced 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.