6.4. Using the Control State with Custom Controls
You want to allow users to disable the
ViewState for your custom control but you do not want to lose functionality when they do. In other words, when a developer disables the
ViewState for the page or your custom control, you want to be able to maintain critical state information for your control.
Create a custom control like the one described in Recipe 6.3, register the control with the
Page indicating the control requires use of the
Control State, override the
SaveControlState method to save the critical information for your control in the
Control State, and override the
LoadControlState method to reload the critical information from the
Control State on
Use the .NET language of your choice to:
Create a class that inherits from the
WebControlclass in the
Implement state support as described in Recipe 6.3.
In the control
Initmethod, register the control with the
Pageindicating the control requires use of the
SaveControlStatemethod to save the critical information for your control in the
LoadControlStatemethod to reload the control information from the
To use the custom control in an ASP.NET page:
Register the assembly containing the control.
Insert the tag for the custom control anywhere in the page and set the attributes appropriately.