Click here to Skip to main content
15,868,164 members
Articles / Web Development / ASP.NET

StateManagement Suggestions : A Beginner's View

Rate me:
Please Sign up or sign in to vote.
3.71/5 (7 votes)
18 Mar 2010CPOL7 min read 18.5K   17   3
This article takes you on a short trip of State Management techniques used in ASP.NET.

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:

  1. How much information do you need to store?
  2. Do you want to store the information on the client or on the server?
  3. What are the capabilities of the browsers that you are targeting?
  4. How long do you need to store the information?

Types of State Management Techniques?

There are two types of statement management:

  1. Client side statement management
  2. 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:

  1. No server resources are required
  2. Simple implementation
  3. Performance considerations
  4. 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:

  1. No server resources are required.
  2. Reliability - As this state cannot be turned off, control state is a more reliable method for managing the state of controls.
  3. 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:

  1. No server resources are required
  2. Widespread support - Almost all browsers support
  3. Simple storage architecture

There are some disadvantages too:

  1. Storage limitations
  2. Performance considerations - storing large values can cause the page to slow down
  3. The hidden field does not support rich data types
  4. 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:

  1. No server resources are required
  2. Configurable expiration - We can set the cookie expiration period
  3. Simple to use
  4. 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:

  1. Easy to implement
  2. Wide range of support from browser
  3. No server resource needed

It has some disadvantages:

  1. Complex data cannot transfer
  2. URL Length limitation problem
  3. 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:

  1. Application state is easy to implement and use.
  2. Application state is accessible to all pages in an application, it means keeping only a single copy of the information.
  3. 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:

  1. User specific data can be handled
  2. Platform scalability
  3. Cookie less session - Session state works with browsers that do not support HTTP cookies
  4. 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:

  1. Data Persistence - Data placed in profile properties is preserved through IIS restarts
  2. Extensibility

It has some disadvantages:

  1. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Hi there, I am Prasad. Author, Blogger, contributor and passionate about Microsoft .NET technologies. I like to write an articles/blogs on different .NET aspects and like to help Developers, to resolve their issues and boost them on Microsoft Technologies.


Certifications: Microsoft Certified professional (MCP), Microsoft Certified technology specialist (MCTS), Agile-Scrum Master.


Awards: Microsoft Re-connect MVP (GSC Member), Most valuable member at dotnetspider, Most popular curator, Most active curator, featured curator at Microsoft Curah, Editor at dotnetspider.


Microsoft MVP 2014 [ASP.NET/IIS]
Click here for more .NET Tips
-After all Knowledge is an endless entity

Comments and Discussions

 
Questiongood Pin
iaskalyani4511-Jan-13 16:06
iaskalyani4511-Jan-13 16:06 
GeneralMy vote of 1 Pin
Sandesh M Patil7-Sep-10 3:45
Sandesh M Patil7-Sep-10 3:45 
Find below one comment
GeneralStateManagement Suggestions : A Beginners View Pin
Fregate22-Mar-10 13:18
Fregate22-Mar-10 13:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.