As the follow up to previous post, I’ll tell you about the improved version of ViewState in ASP.NET 2.0 today, ControlState. In fact, I never wrote a control that uses ControlState myself but I’ve read about it once. It takes a lot of effort compare to ViewState and I think it’s why there’re not many controls use it.
Using ViewState is as easy as pie. It’s only a indexed collection or you can call it Hash or Dictionary. Throw any serializable object into the ViewState and it will be serialized, encoded and persisted to the web form. That’s all. You don’t have to register any index or anything. Look at this piece of code.
public int _currentIndex
public int CurrentIndex
{
get
{
if (ViewState["CurrentIndex"] != null)
_currentIndex = Convert.ToInt32(ViewState["CurrentIndex"]);
}
return _currentIndex;
}
set
{
ViewState["CurrentIndex"] = value;
_currentIndex = value;
}
}
In contrast to the ease of ViewState, ControlState introduces more overhead and have to be registerd before we can use it. You have to register your control in initialization event and tell how to save and load state from the state object before use it.
Below is an example I copied from MSDN Library. I cut the ViewState section off to help you read it easier.
public class IndexButton : Button
{
private int indexValue;[ Bindable(true),
Category("Behavior"),
DefaultValue(0),
Description("The index stored in control state.") ]
public int Index {
get { return indexValue; }
set { indexValue = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.RegisterRequiresControlState(this);
}
protected override object SaveControlState()
{
object obj = base.SaveControlState();
if (indexValue != 0)
{
if (obj != null)
{
return new Pair(obj, indexValue);
}
else
{ return (indexValue); }
}
else
{ return obj; }
}
protected override void LoadControlState(object state)
{
if (state != null)
{
Pair p = state as Pair;
if (p != null)
{
base.LoadControlState(p.First);
indexValue = (int)p.Second;
}
else
{
if (state is int)
{
indexValue = (int)state;
}
else
{
base.LoadControlState(state);
}
}
}
}
}
Pair is a container class that contains 2 objects. The class that contains 3 object named Triplet. They’re mostly use in ViewState and ControlState state object. I don’t recommend to use them in other purposes or you’ll end up with confusion.
Despite of it overhead, ControlState has been widely use instead of ViewState. Because control state can’t be disabled; as a result, you can guarantee that turning off ViewState doesn’t make your control unworkable. GridView use ControlState over ViewState to store its various indices. I don’t know about the other but I assume that almost all new controls in ASP.NET 2.0 utilize ControlState as well.
Although it can’t be disable, this makes it a 2-edges sword. Remember again, ControlState is the ViewState that cannot be disabled. You have to decide wisely what will be kept in ControlState because your HTML can be bloated with a lot of ControlState data. Stay away from both ViewState and ControlState, if you can.
