Seven Status Management
It can be seen from the control's lifecycle that, in the view State saving stage, the page recursively calls the saveviewstate method for each control in the control tree. Then, each control returns an object that contains the information required by the control to restore the status in the return request. In the generation phase, the page uses an instance of the system. Web. UI. losformatter class to serialize the view status into a string representation, and then forwards the view status back and forth to the client in the _ viewstate hidden domain.
By default, the contorl class delegates state management to the viewstate attribute, which is a complex attribute and is a statebag type and a dictionary, the statebag class implements the istatemanager interface for status management. Statebag stores key/value pairs. The key and value pairs are strings and the value is objects. When an object is added to the statebag instance, stagebag automatically stores an additional bit for the object, which indicates whether the stored object has been modified.
The control class is used for status management by the same members in the istatemanager interface. Although the control class does not implement the istatemanager interface, it is only delegated to the statebag class that implements the istatemanager interface type, that is, the viewstate attribute is delegated. In your own complex type State management, you can simulate the behavior of control to achieve custom State management of complex attributes.
1. Istatemanager Interface
Public interface istatemanager
{
Bool istrackingviewstate {Get ;}
Void loadviewstate (Object State );
Object saveviewstate ();
Void trackviewstate ();
}
First, we will analyze how the control class manages the default state by entrusting the viewstate attribute:
Private statebag _ viewstate;
Protected virtual statebag viewstate
{
Get
{
If (_ viewstate! = NULL) {return _ viewstate ;}
_ Viewstate = new statebag (viewstateignorescase );
If (istrackingviewstate)
_ Viewstate. istrackingviewstate ();
Return _ viewstate;
}
}
Protected virtual void trackviewstate ()
{
If (_ viewstate! = NULL)
{
_ Viewstate. trackviewstate ();
}
}
Protected virtual object saveviewstate ()
{
If (_ viewstate! = NULL)
{
Return _ viewstate. saveviewstate ();
}
Return NULL;
}
Protected virtual void loadviewstate (Object savedstate)
{
If (_ viewstate! = NULL)
{
Viewstate. loadviewstate (savedstate );
}
}
When attributes are saved in viewstate, their statuses are automatically maintained. Any Type stored in viewstate must be serialized through losformatter.
When a control defines complex attributes, it is generally impossible to use the viewstate dictionary to manage the statuses of these attributes. to indicate the custom types of complex attributes, you can use the istatemanager interface to manage the statuses.