6.3 Control state Mechanism
The control state mechanism has been supported since ASP.net 2.0. Control's state data can now be persisted through control state instead of view state, and control state cannot be disabled as view state. Because the state of the control works in exactly the same way as the view state, and by default it is stored in the same hidden field on the page, there is little introduction to how it works.
Similar to LoadViewState and saveviewstate, control states provide a pair of methods, the name of the method is loadcontrolstate and Savecontrol state, and are also provided in the controls base class.
The shaded portion of Figure 6-5 is the stage of control state in the control lifecycle.
If you enable the control state feature, you only need to implement the following methods:
/// <summary>
/// 获得本书更多内容,请看:
/// http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </summary>
public class ControlStatePeriod : WebControl
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.RegisterRequiresControlState(this);
}
protected override object SaveControlState()
{
//… …
}
protected override void LoadControlState(object savedState)
{
//… …
}
}