ASP.NET Client Side State Management
There are two types of state management - server side and client side. In this post I'm going to introduce the client side state management and explain when to choose client side or server side state management.
Client Side State Management Techniques
Client side state management include the following techniques:
- ViewState - ASP.NET track the state of the controls on the pages through
the ViewState. Also, ViewState is used to store small custom values.
Don't hold big custom data object because it will affect the performance of
your web page.
- Hidden fields - hidden fields are html input control with a type of hidden -
<input type="hidden">. They are used to store data in the html without presenting
the data in the browser. The data of hidden field is available when the form is
data. You can use the View Source operation (if enabled) to scan the web page's
source and to look for hidden fields.
- Query strings - query string state is stored in the URL that is sent to the browser.
Unlike ViewState and hidden fields, the user can see the values without using
special operations like View Source. The parameters are passed in the URL
separated by the & symbol.
For example, in the following URL the query string is built out of two data
parameters, a and b, which hold the values 1 and 2 - http://www.srl.co.il?a=1;b=2.
- Cookies - the cookies store their values in the user's browser and the browser
send them back to the server every time a request is performed.
Cookies are maintained during the session of a user in the web site and therefore
can be used in multiple web pages of the site.
- Control state - when building custom controls you should use the control state
to save your state if EnableViewState property is set to false.
This is the only reason to use this technique.
How do you know when to choose the client side or server side state management?
When to Choose Client Side State Management
Choose client side for better scalability and support multiple servers. The client side helps to get better scalability by storing state data in the user's browser instead of using the web server's memory. The client side support multiple servers because all the state is located in the browser. This way when you are redirected to another server you don't need to worry about your state.
The thing I wrote doesn't means that you can't use server side state management for the supporting of multiple server. To enable server side state management to be able to support multiple servers you'll have to use centralized state management (state server or store state in database) or you'll have to use techniques like sticky
connection for load balancing.
When to Choose Server Side State Management
Choose server side for better security and to reduce bandwidth and web page's size. Server side state management is more secure. The state is saved on the server and therefore isn't delivered to the client.
Confidential state data shouldn't be used with client side state management. Server side reduce the traffic to and from the client because data isn't sent to the browser and it's saved on the server. You should always remember that using client side state management sends data to the user's browser and that data is sent back to the server
every time. This situation increases bandwidth usage and therefore your application will be less responsive and you'll suffer from performance issues.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)