90 likes | 190 Views
ASP.NET Page State. Wallace B. McClure Scalable Development, Inc. ASP.NET Podcast http://www.aspnetpodcast.com/. State. ViewState ViewState is the mechanism in ASP.NET to keep page state between postbacks. Used to cause page level events such as button clicks.
E N D
ASP.NET Page State Wallace B. McClure Scalable Development, Inc. ASP.NET Podcast http://www.aspnetpodcast.com/
State • ViewState • ViewState is the mechanism in ASP.NET to keep page state between postbacks. • Used to cause page level events such as button clicks. • Hidden form element – default. • ControlState • Control State data specific to a control • Separate from ViewState.
Problem • ViewState size can be rather large. If it is sent between the server and client, it can significant take time to transfer the data. • 300K-500K-1000K is not uncommon. • Turning off ViewState can break controls with special state needs, thus ControlState is separate from ViewState.
Solution • Store ViewState/ControlState someplace besides a hidden formfield element. • Alternative locations: • Webserver – How do you handle a webfarm? • Database • Problems: • Cleanup. • Back button. • AJAX.
Load/Save Data • Inherit from the PageStatePersister. • Override Load()/Save()
State() • Obtain ViewState/ControlState. • Save to a Pair object. • Serialize the Pair to a string. • Save the string. • Register the unique identifier. • PK in a database. • Filename in the file system.
Load() • Read the unique identifier. • Cleanup old values. • Obtain the serialized pair object. • Deserialize the pair. • Set the ViewState/ControlState properties.
Final Thoughts • Deleting the previous value destroys the back button functionality. • Deleting old values. • Uncontrolled growth. • What happens with AJAX calls.