Monday, 8 December 2014

State Management

Different types of state management?
There are two different types of state management:

Client Side State Management
View State
Hidden Field
Cookies
Control State

Server Side State Management
Session
Application Object
Caching
Database

Client Side state management does not use any server resource , it store information using client side option. Server Side state management use server side resource for store data. Selection of client side and server side state management should be based on your requirements and the selection criteria that are already given.

What is view state?
View State is one of the most important and useful client side state management mechanism. It can store the page value at the time of post back (Sending and Receiving information from Server) of your page. ASP.NET pages provide the ViewState property as a built-in structure for automatically storing values between multiple requests for the same page.

Example:

If you want to add one variable in View State,

 ViewState["Var"]=Count;
For Retrieving information from View State


string Test=ViewState["TestVal"];
Sometimes you may need to typecast ViewState Value to retreive. As I give an Example to strore and retreive object in view state  in the last of  this article.

Advantages of view state?
This are the main advantage of using View State:
Easy to implement
No server resources are required
Enhanced security features ,like it can be encoded and compressed

Disadvantages of view state?
This are the main disadvantages of using View State:

It can be performance overhead if we are going to store larger amount of data , because it is associated with page only.
Its stored in a hidden filed in hashed format (which I have discussed later) still it can be easily trapped.
It does not have any support on mobile devices.

When we should use view state?
I already describe the criteria of selecting State management. A few point you should remember when you select view state for maintain your page state.

Size of data should be small , because data are bind with page controls , so for larger amount of data it can be cause of performance overhead.
Try to avoid storing secure data in view state

When we should avoid view state?
You won't need view state for a control for following cases,
The control never change
The control is repopulated on every postback
The control is an input control and it changes only of user actions.
Where is view state stored?
View State stored the value of page controls as a string which is hashed and encoded in some hashing and encoding technology. It only contain information about page and its controls. Its does not have any interaction with server. It stays along with the page in the Client Browser. View State use Hidden field to store its information in a encoding format.

QuestionsAnswer
Client Side or Server Side ?Client Side
Use Server Resource ?No
Easy to implement ?Yes
Cause Performance Issue ?For heavy data and case of encryption & decryption
Support Encryption Decryption?Yes
Can store objects ?Yes, but you need to serialize the class.
TimeoutNo

No comments:

Post a Comment