Share via


What's New in ASP.NET State Management

ASP.NET version 2.0 retains much of the functionality of ASP.NET version 1.1, including automatic page-state persistence using view state, and database support for session and application state. Additionally, ASP.NET 2.0 adds two new features: view-state chunking and control state.

Control State

Sometimes you need to store control-state data in order for a control to work properly. For example, if you have written a custom control that has different tabs that show different information, in order for that control to work as expected, somehow the control needs to know which tab is selected between round trips. The ViewState property can be used for this purpose, but view state can be turned off at a page level by developers, effectively breaking your control. To solve this, the ASP.NET page framework exposes a new feature in ASP.NET version 2.0, called control state.

The ControlState property allows you to persist property information that is specific to a control and cannot be turned off like the ViewState property. To use control state, your control must call the RegisterRequiresControlState method during initialization and then override the SaveControlState and LoadControlState methods.

Profile Properties

Many times, you need to store user-specific data in order to customize the experience of a user when they use your application. There are two major ways to accomplish this in ASP.NET 1.1. Session state provides an easy way to store user-specific information. However, session state is removed from memory when the user session expires. You can also use cookies to store a unique user identifier, and then persist and retrieve information from a database. However, this requires custom code. To solve this, ASP.NET 2.0 provides a new feature called profile properties, which allows you to store user-specific data.

The profile properties feature is similar to session state, except that the profile data is not lost when a user session expires. Instead, the profile properties feature uses an ASP.NET profile that is stored in a persistent format and associated with an individual user. The ASP.NET profile allows you to manage user information without requiring that you create and maintain your own database. In addition, the profile makes the user information available using a strongly typed API that you can access from anywhere in your application. You can store objects of any type in the profile. The ASP.NET profile feature provides a generic storage system that allows you to define and maintain almost any kind of data while still making the data available in a type-safe manner.

For more information, see ASP.NET Profile Properties Overview.

View-State Chunking

View state provides an easy way to automatically persist field and control data on the page without having to manually request it and then re-populate during round trips to the server. It also allows you to store custom data on the page in the ViewState property.

However, view-state data can, in some cases, become very large. Because view-state data is stored in hidden fields, some proxies and firewalls will prevent access to the page that contains them. For this reason, the ASP.NET 2.0 page framework introduces a feature called view-state chunking. If the amount of view-state data becomes too large, view-state chunking will automatically split the data into chunks and put the data into multiple hidden-form fields.

To enable view-state chunking, set the MaxPageStateFieldLength property to the maximum size (in bytes) that you want to allow in a single view-state field. When the page is posted back to the server, the page parses the view-state string at page initialization and restores property information in the page. The default setting is -1, which means that there is no maximum size and the view state will not be broken up into chunks.

For more information, see View State Overview.

Custom Session-State Management

By default, session-state values and information are stored in memory within the ASP.NET process. ASP.NET also provides session-state providers that allow you to use a session-state server that keeps session data in a separate process, or you can persist session state data to a SQL database. However, with ASP.NET 2.0, you can create custom session-state providers that allow you to customize how session-state data is stored in your ASP.NET applications. For example, you might consider creating a custom provider for the following reasons:

  • You need to store session-state information in a data source other than SQL Server, such as a Visual FoxPro database or an Oracle database.

  • You need to manage session-state information using a database schema that is different from the database schema used by the providers that ship with the .NET Framework. An example of this would be shopping cart data that is stored with a predefined schema in an existing a SQL Server database for a company or Web site.

For more information, see Implementing a Session-State Store Provider.

The HiddenField Control

View state gives you the programmatic ability to store data on a Web page and it automatically retrieves, persists, and maintains that data in hidden form fields on the page. However, ASP.NET 2.0 adds a new Web control, the HiddenField control that renders as an input type="hidden"/ element but gives you an API that is consistent with other Web controls.

See Also

Concepts

ASP.NET State Management Overview
ASP.NET State Management Recommendations
ASP.NET Cookies Overview
View State Overview
Session State Overview
ASP.NET Application State Overview