6.6 Clear Page Status
Sometimes you need to clear the page state of a child control when it is developed, such as when you create a child control, which is a scenario that clears the view and creates a child control at data binding time:
/// <summary>
/// 获得本书更多内容,请看:
/// http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </summary>
public override void DataBind()
{
base.OnDataBinding(EventArgs.Empty);
Controls.Clear();
ClearChildViewState(); //清理视图状态
TrackViewState();
CreateControlHierarchy(true);
ChildControlsCreated = true;
}
This section describes several ways to clean up the page state, primarily provided by the control base class.
1. Haschildviewstate method
Gets a value that indicates whether the child control of the current server control has any saved view state settings. Returns true if any child controls have saved view state information, otherwise false.
2. Ischildcontrolstatecleared method
Gets a value that indicates whether the control contained in the current control has control state. True if the control's child does not use control state, otherwise it returns false.
3. Clearchildstate method
Clears both the view state and the control state of the child control. The method body is as follows:
protected void ClearChildState()
{
this.ClearChildControlState();
this.ClearChildViewState();
}
4.ClearChildControlState方法
清除控件状态。其代码如下:
protected void ClearChildControlState()
{
if (this.ControlState >= ControlState.Initialized)
{
this.flags.Set(0x40000);
if (this.Page != null)
{
this.Page.RegisterRequiresClearChildControlState(this);
}
}
}