Control development, sometimes you need to clear the page state of a child control, for example, when you create a child control, the following is a scenario in which the view is purged and the child controls are created when the data is bound:
public override void DataBind ()
{
Base. OnDataBinding (Eventargs.empty);
Controls.clear ();
Clearchildviewstate (); Clean up View state
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 method
Clears the control state. 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);
}
}
}
Control state is through the method:
Page.registerrequirescontrolstate (this);
Registers a control as a control with persistent control state. You also invoke the method:
This. Page.registerrequiresclearchildcontrolstate (this);
Clears the control state, which is different: This method is an internal method. is not invoked by the developer and can only be invoked indirectly through the Clearchild ControlState method.
5. Clearchildviewstate method
This method mainly clears the view state of the child control, and its code structure is as follows:
protected void Clearchildviewstate ()
{
if (this._occasionalfields!= null)
{
This._occasionalfields.controlsviewstate = null;
}
}
The IDictionary object that sets control view state in the method body controlsviewstate to null.