ionVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public Address CustomAddress
{
get
{
if (address == null)
{
address = new Address();
if (IsTrackingViewState)
{
((IStateManager)address).TrackViewState();
}
}
return address;
}
}
protected override void LoadViewState(object savedState)
{
Pair p = savedState as Pair;
if (p != null)
{
base.LoadViewState(p.First);
((IStateManager)CustomAddress).LoadViewState(p.Second);
return;
}
base.LoadViewState(savedState);
}
protected override object SaveViewState()
{
object baseState = base.SaveViewState();
object thisState = null;
if (address != null)
{
thisState = ((IStateManager)address).SaveViewState();
}
if (thisState != null)
{
return new Pair(baseState, thisState);
}
else
{
return baseState;
}
}
protected override void TrackViewState()
{
if (address != null)
{
((IStateManager)address).TrackViewState();
}
base.TrackViewState();
}
#endregion有几个地方,要请大家注意
一点,视图状态的操作包括基类base的操作,还需要调用相应的自定义属性的类型的状态维护方法。
二点,Load和Save是一个完全相反的操作过程。
三点,Load和Save保持数据的Pair,可以用数组Array代替,如果是两个自定义属性,怎么办呢?当然是Priple,如果是三个呢?怎么办?请大家自己想。
四点,要理解视图状态,先理解她的生命周期,免得有时候,竹篮打水,一场空。
五点,CustomAddress属性为只读, ^-^