Custom state management for ASP. net2.0 server controls

Source: Internet
Author: User

Previous SeriesArticleWe have 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 has an implementationCodeActually, it is 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.

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.