2010年10月9日 星期六

伺服器控制項與子屬性的視圖狀態比較

















public class BookNew : WebControl
public class StateManagedAuthor : IStateManager
     
private StateManagedAuthor authorValue;

[DesignerSerializationVisibility(
DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty)]
public virtual StateManagedAuthor Author
{
get
{
if (authorValue == null)
{
authorValue = new StateManagedAuthor();

if (IsTrackingViewState)
{
((IStateManager)authorValue).TrackViewState();
}
}
return authorValue;
}
}
private bool _isTrackingViewState;
private StateBag _viewState;

protected virtual StateBag ViewState
{
get
{
if (_viewState == null)
{
_viewState = new StateBag(false);

if (_isTrackingViewState)
{
((IStateManager)_viewState).TrackViewState();
}
}
return _viewState;
}
}
bool IStateManager.IsTrackingViewState
{
get
{
return _isTrackingViewState;
}
}
internal void SetDirty()
{
_viewState.SetDirty(true);
}
protected override void LoadViewState(object savedState)
{
Pair p = savedState as Pair;
if (p != null)
{
base.LoadViewState(p.First);
((IStateManager)Author).LoadViewState(p.Second);
return;
}
base.LoadViewState(savedState);
}
void IStateManager.LoadViewState(object savedState)
{
if (savedState != null)
{
((IStateManager)ViewState).LoadViewState(savedState);
}
}

protected override object SaveViewState()
{
object baseState = base.SaveViewState();
object thisState = null;

if (authorValue != null)
{
thisState = ((IStateManager)authorValue).SaveViewState();
}

if (thisState != null)
{
return new Pair(baseState, thisState);
}
else
{
return baseState;
}
}
object IStateManager.SaveViewState()
{
object savedState = null;

if (_viewState != null)
{
savedState =((IStateManager)_viewState).SaveViewState();
}
return savedState;
}
protected override void TrackViewState()
{
if (authorValue != null)
{
((IStateManager)authorValue).TrackViewState();
}
base.TrackViewState();
}
void IStateManager.TrackViewState()
{
_isTrackingViewState = true;

if (_viewState != null)
{
((IStateManager)_viewState).TrackViewState();
}
}
public override string ToString()
{
return ToString(CultureInfo.InvariantCulture);
}
public string ToString(CultureInfo culture)
{
return TypeDescriptor.GetConverter(
GetType()).ConvertToString(null, culture, this);
}

參考資料:


沒有留言:

張貼留言