ASP. NET custom server control internal details series tutorial 7 (State Management)

Source: Internet
Author: User

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.

 

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.