Asp. NET in the control base class Cleanup page state

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.