Exforsys.com
 
Home Tutorials ASP.NET 2.0
 

ASP.NET 2.0 Free Tutorials : State Management in ASP.NET 2.0

 

ASP.NET 2.0 Free Tutorials : State Management in ASP.NET 2.0 - Page 2

Page 2 of 2


Implementing the control state:

The implementation of the control state is prerogative of the programmer. The process may be as complex as implementing serialization and deserialization for the control’s state or a mere tweaking of the control’s code to achieve optimal performance in the context of usage. The initialization and loading of a controls private state has to be done in three steps:


  • Override the OnInit method and invoke the System.Web.UI.Page.RegisterRequiresControlState(System.Web.UI.Control) method to register with the page for participation in control state.
  • Override the SaveControlState method to save data in control state.
  • Override the LoadControlState method to load data from control state.

Let us assume that we want to create a button called IndexButton. The custom control saves its state both in the control state and the view state. The IndexButton derives from the Button class and defines an Index property that is saved in control state. The IndexButton also defines an IndexViewState property that is stored in the ViewState dictionary.


Click here to view sample code


Now let us test this in the default.aspx page. Let us disable the view state by setting the EnableViewState attribute of the page to false in the page directive. In the Page_Load event handler let us add the value of the Index and IndexInViewState property in the IndexButton control. It must be noted that since the Index property is stored in the control state(and cannot be disabled), it maintains its value on postback and increases by one each time the page is posted back to the server. Since the IndexViewState property is stored in the view state and is disabled for the page, the IndexViewState property is always zero.


Click here to view sample code


New data structures can be allocated (arrays of objects, a hashtable or a custom type) and filled with the private properties to persist across the postbacks. When the method terminates it returns this object to ASP.NET runtime. The object is then binary serialized and encoded to a Base64 stream. It follows the class used to collect the control state properties must be serializable.


It is clear that the control state features were introduced to avoid the common errors made by programmers in control development. In the next section we shall examine the session state and how it has been extended in ASP.NET 2.0.




First Page: ASP.NET 2.0 Free Tutorials : State Management in ASP.NET 2.0


Read Next: ASP.NET 2.0 Training : Customizing the Session State Mechanism



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape