Asp. NET State Management 12 (control state controlstate)

Source: Internet
Author: User

If you are writing a control, you can store the state information of the control in the ViewState dictionary, which is a StateBag object. The developer will retrieve the control state through the ControlState property. You specify the key and value for the ViewState property, and the Page object serializes the state information between requests. To perform custom state processing in your control, you can override the LoadViewState and SaveViewState methods. When a page developer disables view state, all state information stored in this dictionary is lost. To compensate for this flaw, in asp.net version 2.0, you can store critical state information in a separate object called control state. When a page developer disables view state, the control state object is not affected. To store state information in a control state object, the control needs to override the LoadControlState and SaveControlState methods, and it needs to register it to store state information in the control state each time the control is initialized. You can register a control to use control state by overriding the OnInit method and calling the Registerrequirescontrolstate method.

Control state is an object that contains the critical view state data that a WEB server control needs to work, and it is contained in a separate object that is different from the object that contains the normal view state information. When view state is disabled at the Page level, control state data is not affected, but additional implementation steps are required to be used.

The following code example shows how a class derived from the PageStatePersister class initializes the ControlState property. This example assigns the ControlState property to the Second field of the Pair object and serializes the property using the ObjectStateFormatter class. When the Load method is invoked, the ObjectStateFormatter class is deserialized with view state and control state information, and the ControlState property is initialized with the Second field of the resulting Pair object.


//Load ViewState and ControlState.
Public
override void Load ()
{
Stream statestream = Getsecurestream ();
Read the state string, using the Stateformatter.
StreamReader reader = new StreamReader (statestream);
Istateformatter formatter = this.  Stateformatter;
String filecontents = reader.  ReadToEnd ();
Deserilize returns the Pair object is serialized in
//The Save method.
Pair Statepair = (Pair) formatter.  Deserialize (filecontents);
ViewState = Statepair.first;
ControlState = Statepair.second;
Reader.  Close ();
Statestream.close ();
}

Http://aierong.cnblogs.com

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.