During control development, you sometimes need to clear the Page Status of the Child control. For example, when creating a child control, the following is an Application Scenario for clearing the view and creating a child control during data binding:
Public override void DataBind ()
{
Base. OnDataBinding (EventArgs. Empty );
Controls. Clear ();
ClearChildViewState (); // clear view status
TrackViewState ();
CreateControlHierarchy (true );
ChildControlsCreated = true;
}
This section describes several methods to clean up the page status, which are mainly 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 status settings. If any child control has saved view status information, true is returned; otherwise, false is returned.
2. IsChildControlStateCleared Method
Gets a value that indicates whether the control contained in the current control has the control status. If the control status is not used for its child level, true is returned; otherwise, false is returned.
3. ClearChildState Method
The view status and control status of the sub-control are also cleared. The method is as follows:
Protected void ClearChildState ()
{
This. ClearChildControlState ();
This. ClearChildViewState ();
}
4. ClearChildControlState Method
Clear the control status. The Code is as follows:
Protected void ClearChildControlState ()
{
If (this. ControlState> = ControlState. Initialized)
{
This. flags. Set (0x40000 );
If (this. Page! = Null)
{
This. Page. RegisterRequiresClearChildControlState (this );
}
}
}
The control status is through the method:
Page. RegisterRequiresControlState (this );
Register a control as a control in the persistent control state. You must also call the following methods:
This. Page. RegisterRequiresClearChildControlState (this );
Clears the control status. The difference is that this method is an internal method. It is not called by developers and can only be indirectly called through the ClearChild ControlState method.
5. ClearChildViewState Method
This method mainly clears the view status of the child control. The code structure is as follows:
Protected void ClearChildViewState ()
{
If (this. _ occasionalFields! = Null)
{
This. _ occasionalFields. ControlsViewState = null;
}
}
The IDictionary object ControlsViewState for setting the control view status in the method body is null.