Introduction
State management is the process by which we maintain state and page information over multiple requests for the same or different pages. Pages are destroyed and re-created with each round trip to the server; therefore, page information will not exist beyond the life cycle of a single page. ASP.NET provides multiple ways to maintain state between server round trips. Which of these options you choose depends heavily upon your application.
Background
There are many ways in which we can transfer the values from one page to another. Many new beginners do not know about it. So this is the short attempt to give an idea about the state management technique to them.
Which State Management Technique to be Used ?
This is the main and difficult question. Now this is heavily dependant upon the following things:
- How much information do you need to store?
- Do you want to store the information on the client or on the server?
- What are the capabilities of the browsers that you are targeting?
- How long do you need to store the information?
Types of State Management Techniques?
There are two types of statement management:
- Client side statement management
- Server side statement management
Furthermore, Client side state management can be handled through the following ways. We will discuss them in detail later.
- View state
- Control state
- Hidden fields
- Cookies
- Query strings
The above options typically have minimal security, but fast server performance.
Client Side State Management
ViewState
Web Forms pages have the ViewState
property as a built-in structure, for automatically retaining values between multiple postbacks for the same page. You can use view state to store your own page-specific values across round trips. We can turn off viewstate for a specific page. The viewstate has some bold features as follows:
- No server resources are required
- Simple implementation
- Performance considerations
- Does not require any custom coding
Recommended usage: Use when you need to store small amounts of information for a page.
Control State
By using control state, we can persist custom control data between multiple postbacks. We cannot turn off control state. It is a very reliable method for managing the state of control. Let's take a look at some advantages:
- No server resources are required.
- Reliability - As this state cannot be turned off, control state is a more reliable method for managing the state of controls.
- Versatility - We can configure, how and where control-state data is stored.
Recommended usage: Use when you need to store small amounts of state information for a control between round trips to the server.
Hidden Fields
User specific information can get stored in hidden fields, hidden field maintains the state between postbacks. It is best to store only small amounts of frequently changed data on the client. Here are the advantages:
- No server resources are required
- Widespread support - Almost all browsers support
- Simple storage architecture
There are some disadvantages too:
- Storage limitations
- Performance considerations - storing large values can cause the page to slow down
- The hidden field does not support rich data types
- Potential security risks - The information in the hidden field can be seen if the page output source is viewed directly
Recommended usage: Use when you need to store small amounts of information for a page (when security is not an issue).
Cookies
Cookies are the small text files that are stored on client side. It is useful for frequently changed information on the client. We can create cookies. There exist Persistent and Non Persistent cookies. Persistent cookies have an expiration period. Here are some advantages:
- No server resources are required
- Configurable expiration - We can set the cookie expiration period
- Simple to use
- No customize coding needed
Recommended usage: Use when you need to store small amounts of information for a page (when security is not an issue).
Query Strings
A Querystring
is the information that is appended to the URL with the '?' sign. It provides us with a simple but limited way of transferring data. Here are some advantages:
- Easy to implement
- Wide range of support from browser
- No server resource needed
It has some disadvantages:
- Complex data cannot transfer
- URL Length limitation problem
- As the URL is visible to user, it raises a security risk
Recommended usage: Use when you need to store small amounts of information for a page (when security is not an issue).
Server Side State Management
Server-side options for storing page information typically have higher security than client-side options. They can use more Web server resources than client side state management technique. Server side state management can be handled through the following ways. We will discuss them in detail later.
- Application state
- Session state
- Profile properties
Let's start with Application state.
Application State
ASP.NET gives a way to store information that is easily accessible to the entire application and that is using Application state. You can store your application-specific values in application state, which is then managed by the server. Application state is useful for storing information that needs to be maintained between server round trips and between requests for pages. Application state gets initialized in Global.Asax. Some advantages are given below:
- Application state is easy to implement and use.
- Application state is accessible to all pages in an application, it means keeping only a single copy of the information.
- Possibility of Loss of data occurs if server crashes, is upgraded, or shutdown.
Recommended usage: Use when you are storing infrequently changed, global information that is used by many users.
Session State
ASP.NET allows us to store user specific information using Session State. It provides the ability to persist variable values between multiple postbacks. ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. Session variables are stored in a SessionStateItemCollection
object that is exposed through the HttpContext..::.Session
property. Session variables are created by referring to the session variable by name. You do not have to declare a session variable or explicitly add it to the collection. Some advantages are given below:
- User specific data can be handled
- Platform scalability
- Cookie less session - Session state works with browsers that do not support HTTP cookies
- Huge range of datatype support
Recommended usage: Use when you are storing short-lived information that is specific to an individual user and security is an issue.
Profile Properties
Profile properties allow you to store user-specific data. It is similar to session state, but the profile data is not lost when a user's session expires. The profile properties are stored in a persistent format and associated with an individual user. The ASP.NET profile allows you to easily manage user information without requiring you to create and maintain your own database. Some advantages are given below:
- Data Persistence - Data placed in profile properties is preserved through IIS restarts
- Extensibility
It has some disadvantages:
- Data maintenance - Profile data is persisted to non-volatile storage, you must make sure that your application calls the appropriate cleanup mechanisms
Recommended usage: Use when you are storing user-specific information that needs to be persisted after the user session is expired. Let's take a snap check.
History
To transfer the data from a web page to another web page, we have to persist the state, for the same we have used State Management techniques. It has two parts, Client side state management and Server side state management. We can keep track of Client side state management using View state, Control state, Hidden fields, Cookies, Query strings. - We can keep track of Server side state management using Application, Session, Profile properties. This is a short trip of state management.
You are most welcome to make suggestions and comments and I hope they will help me to improve this article.