alt
Advertisement
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow ASP.NET 2.0 arrow ASP.NET 2.0 Free Tutorials : State Management in ASP.NET 2.0
Site Search


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

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

In this tutorial you will learn about new features included in ASP.NET 2.0 for State Management. The Control State, differences in handling View State and Control State, Implementing the control state. Initialization and loading of a controls private state.

Web pages are constantly constructed and destroyed with round trips to the server. However, state maintenance is an essential aspect of deploying web pages on a stateless protocol such as HTTP. State management is defined as a process of maintaining state and page information over multiple page requests.

State maintenance could be a feature of a client side or a server side process. The approach to state maintenance within an application will depend on a number of factors such as sensitivity of data, storage of data, performance goals and so on. ASP.NET 2.0 state maintenance can be enforced from both the client and the server effectively.

On the client side the developer has options of View state, hidden fields and cookies. The ViewState property provides a dictionary object for retaining values between multiple requests for the same page. The Hidden field stores a variable in its value property and is explicitly added to the page. The HTMLInputHidden control provides the hidden field property to the page. A cookie is a small amount of data that is stored in the client machine or in the memory of a client browser session. It contains site specific information that the server sends to the client along with a page output. Cookies can be temporary or have specific expiration dates. Client side storage would imply weakened security and a limitation on the amount of data that can be stored but better performance.

The server side options would include Application, session and Cache. Application state is a key value dictionary structure that is created during the page request to a URL. Application specific information can be added to this structure and stored between page requests. The session state is scoped to the current browser session and the number of session states will be determined by the number of users using the application. The session can be different for each visit by the user. The Cache is a hashtable used to store frequently accessed data. It is global to the application and is visible from within each currently active session. The Cache can be accessed using the name/value properties. While session is a block of memories set aside for each user, cache manages globally accessible data. Server side storage would provide for greater security but would have issues of scalability if size of information is large.

The entire state management therefore, will have to be a balance between security and performance.

ASP.NET 2.0 has introduced several new features to help the developer.

1. Custom controls with Control state which are more reliable than View State. This control is independent of the view state and customizable.

2. Custom session state management options which are persistent in the data storage medium and a session state provider object can be created.

3. Mechanism for handling custom cache dependencies, including SQL Server database dependencies in with a feature to invalidate cache results when one of the records displayed gets updated.

The control state

All controls in ASP.NET inherit the View State property from the Control Class. The state is stored in Name/value pairs and the view state property returns an instance of the StateBag class which is a specialized dictionary object. At a level of abstraction the Control state and the View state are similar but have several differences in handling.

View State

Control State

View state is a property inherited from the control class and is inherited by all controls. It is independent of the Control’s state.

Control state is the control’s private view state. Control state is designed for storing a control's essential data (such as the page number of a pager control) that must be available on postback when view state is disabled.

View state can be enabled or disabled for an entire page or for a specific control

Control state cannot be disabled for a control.

View state is sent by the server as a hidden variable in a form, as part of every response to the client, and is returned to the server by the client as part of a postback. However, to reduce bandwidth demand when using mobile controls, ASP.NET does not send a page's view state to the client. Instead, the view state is saved as part of a user's session on the server. Where there is a view state, a hidden field that identifies this page's view state is sent by the server as part of every response to the client, and is returned to the server by the client as part of the next request. However, since the view state for a given page must be kept on the server, it is possible for the current state to be out of synchronization with the current page of the browser, if the user uses the Back feature on the browser to go back in the history.

The control state is stored in the same hidden variable as the view state. Even when view state is disabled control state travels to the client and back to the server in the page. On postback the hidden variable is deserialized and the control state is loaded into each control that is registered for the control state mechanism.

The custom control’s view state is stored by adding to a dictionary object through the ViewState property which is inherited from the Control Class. The ViewState property returns an object of the type StateBag which is a dictionary like type.

In the control state the objects are not stored in a fixed container. The data can be maintained in plain private or protected members. This makes access to data faster and is not mediated by a dictionary object.

Few server controls make private use of the view state and even in those controls the use is limited to specific features.

All controls can use the control state as it is intrinsic to the control.

If a developer wants a control to persist in its state between posts, the view state has to be used privately to store the content of non public properties. However, this will not work if the view state is disabled or the application or page level settings are changed.

The control state replaces the private use of the view state by storing the state within the control.

It is possible to control the amount of data that is moved back and forth between posts.

It is not possible to control the amount of data that is moved back and forth during posts.

The view state of the control/page is automatically initialized and restored by ASP.NET runtime

The developer has to initialize the private state of a control on loading.



 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape