State Management in Asp.net Web form
State Management is the Process by which we can manage the Page Information over multiple request for the same page or different page
Further more Page information is destroyed and recreated over each Roundtrip so the Information is lost for every roundtrip
There are mainly two types of State Management
1 . Client Side State Management
2. Server Side State Management
Client Side State Management
Client Side State Management can be achieved by using
- View State
- Control State
- Hidden Fields
- Cookies
- Query String
View State
View State is very useful Client Side property. It can stores and receive any types of data, information.
When to use View State
View State is used when you need to store data at the time of Post Back
How To Use View State by Example
Create Client Page
On Server Side
Now steps to reproduce
- Submit the button
2. Clear the text Field
3. Click on Restore Button
you will get the same Value To Check this you can refresh the Page and View state will get null
Advantages of View State :-
- Easy to Implement
- No Server Resources are Required
- Enhanced Security Feature
(View State is stored in hashed format)
Dis Advantages of View State :-
- The Information stored on View state can be easily decrypted on Client Page therefore it is not suitable for saving sensitive information.
- Device Limitation
- Store values only for same page only
- Performance is not good if you store large number of View State on a single page and can cause your page to load slowly
Control State : -
Control State is used when you are making custom Control for third party user and you need to Store some information and there View State is turned off
so you cannot store the information in View State. Then you can use Control State which is specific for that Control
Advantages of Control State
Reliable :- As Control State cannot be turned off like View State so it is more reliable to store information then View State.
Dis Advantages of Control State
Some Programming is required
Hidden Fields
Hidden fields as implies are hidden fields and can be used to store some basic information like Productid , EmployeeId. Hidden field does have any security encryption or decryption
Advantages of Hidden Fields
As Hidden Fields are event so you can control all the events of this field
Init , Load , PreRender , Unload , ValueChanged
Practical Example of Hidden Field
your Client Side Code
Server Side Code
Lets see we get the Value of hidden Field before Post Back Event in a similar way you get Dynamic value using Form Submission
Cookies State
Cookies are Stored on your Browser and you can store user preference information like Username, Password etc. on client machines. We need to import namespace called Systen.Web.HttpCookie before we use cookie.
There are two types of cookies mainly
1. Persist Cookies — — Cookies does not have Expire Time
2. Non-Persist Cookie —Cookies have Expire Time
Advantages of Cookies State :-
- Easy To maintain.
- Stored on Client Side Machine
Dis Advantages of Cookies State : -
- If user does not Accept cookies then where will the State be Maintain
- Has Limited Memory of 4 Kb of storage
Query String
Query String is used for Passing values from one page to another Page via Response. Redirect()
Dis Advantages of Query String :-
- Query string has limited memory of 2048
- Not Suitable for Big Messages
- Sensitive data will be exposed on Browser
Implementation of Query String
Client side Parameters will be passed on Button Submission
In a same way you can get the Query String Parameter Value is Second Web form page
By Running ClientPage First and then Click on Submit will redirect here on Second Page with Query String parameter value
Server Side State Management :-
There are two types of Server Side State Management
- Application State
- Session State
- Profile Properties
Application State : -
Multiple User Single State
Usage :-
Suppose you have an application which was accessed by multiple user
User One Click Button which has Zero Value Incremented By One
User Two Click the same button then the value will be incremented to two
Session State : -
Single User Single State
Usage :-
For Saving Client Login information on Server
Dis Advantages :-
Session is Ended when the Browser is Closed
Profile Properties :-
Profile Properties does not lost data when browser is closed cause it is stored for Life time in Sql Server App Data Folder)