In ASP.Net 1.x there is a feature called View State, I think all of you know what the View State is, if not, here follows a short description:
View State
View State is a dictionary where values between post-back can be stored. Think of it as the Session feature but on a page level. The View State preserves page and control property values between round trips (post-backs).
When a page is processed, the state of the page and the controls located on the page will be hashed into a string, and saved to a hidden input field on the page. Sometimes multiple hidden fields can be used if the amount of data stored in the ViewState exceeds the specified value of the MaxPageStateFieldLength property. After a post-back, the page will parse the ViewState string and restore the state to the page or the controls on the page. When you create a page or a control, you can use the ViewState class to save your own data to the ViewState.
ViewState["MyKey"] = myValue;
The ViewState can’t save all kinds of objects. A class that is attributed with the SerializableAttribute, implements the ISerializable interface or has a TypeConverter defined, can be persisted.
Note: Classes that are only attributed with the SerializableAttribute are slower and will generate a larger ViewState than those that have a TypeConverter.
To turn of the ViewState, you can use the Control’s EnableViewState property. If the ViewState will be very large on the page, it could be advisable to disable the ViewState on a page level or on a control level to increase performance. If you disable the ViewState on a page level or on a control level, some of the controls’ features that are useful for a control to work properly can be disabled too. With ASP.Net 2.0 there is a new feature called Control State. The Control State can be used to store the state of a control, and will be separated from the ViewState. If the ViewState of the page is disabled, the controls' state will remain.
Control State
The Control Sate is a new feature added to ASP.Net 2.0, which is similar to the ViewState feature but only on a control level. With the Control State, you can make sure properties are persisted during post-backs and will remain even if the ViewState is disabled for the page. Like the View State, the Control State can’t save all kinds of objects. A class that is attributed with the SerializableAttribute, implements the ISerializable interface or has a TypeConverter defined, can be persisted in the Control State. The following is a list of types that can be added to the Control State:
|
Array |
DateTime |
Int16 |
String |
|
ArrayList |
Double |
Int32 |
String [] |
|
Boolean |
Enum |
null (Nothing) |
System.String.Empty |
|
Byte |
Hashtable |
Pair |
Triplet |
|
Char |
HybridDictionary |
Single |
Type |
|
Color |
IDictionary |
|
|
To use the Control State, you must first enable the Control State feature. It can be done by using the Page class RegisterRequiresControlState method, The RegisterRequiredControlState method can be called from the OnInit event.
public class MyControl : Control
{
protected override void OnInit(EventArgs e)
{
Page.RegisterRequiresControlState(this);
base.OnInit(e);
}
...
}
You also need to override the SaveControlSate and LoadControlState method. Those two methods will save and load the state of the control:
private int _myValue = 0;
protected override object SaveControlState()
{
return (object)this._myValue;
}
protected override void LoadControlState(object state)
{
if (state != null)
{
this.myVlaue = (int)state;
}
}
If you inherit a control that uses its own Control State, and you also want to add your own state to the control, you have to call the base class SaveControlState and LoadControlState, for example:
protected override object SaveControlState()
{
object[] state = new object[3];
state[0] = base.SaveControlState();
state[1] = this._myValue1;
state[2] = this._myValue2;
return state;
}
protected override void LoadControlState(object state)
{
object[] stateTmp = (object[])state;
base.LoadControlState(stateTmp[0]);
this._myValue1 = (string)stateTmp[1];
this._myValue2 = (string)stateTmp[2];
}