以下作品:
每次都创建控件(看起来很糟糕!):
HiddenField hd_IsDirty = new HiddenField();
告诉页面该控件需要一个 ControlState OnInit:
this.Page.RegisterRequiresControlState(this);
覆盖 ControlState 方法:
protected override object SaveControlState()
{
object obj = base.SaveControlState();
if (!string.IsNullOrEmpty(hd_IsDirty.Value))
{
if (obj != null)
{
return new Pair(obj, hd_IsDirty.Value);
}
else
{
return hd_IsDirty.Value;
}
}
else
{
return obj;
}
}
protected override void LoadControlState(object state)
{
if (state != null)
{
Pair p = state as Pair;
if (p != null)
{
base.LoadControlState(p.First);
hd_IsDirty.Value = (string)p.Second;
}
else
{
if (state is string)
{
hd_IsDirty.Value = (string)state;
}
else
{
base.LoadControlState(state);
}
}
}
}