In this article I will explain what is ViewState in ASP.Net. This article will also cover the following information about ViewState: -
1. Where and how does ViewState store data.
2. Scope of ViewState.
3. Uses of ViewState.
4. Benefits of ViewState.
5. Disadvantages of ViewState.
 
 
ViewState
ViewState is a State Management technology in ASP.Net Web Applications which allows the data present in the various Controls and Fields to be preserved across PostBacks.
 
 
Where and how does ViewState store data
ViewState stores the data on the Page itself in a Hidden Field in Hashed format as shown below.
What ViewState in ASP.Net
 
 
Scope of ViewState
ViewState data is only available across PostBacks for the specific Page and is lost when the Page is redirected even redirection is done to the same Page.
 
 
Uses of ViewState
ViewState has the following uses: -
1. Preserve data across PostBack
ViewState is used to preserve data across PostBack. Imagine a long Form where User has to submit Form multiple times for uploading Files, etc. Thus, the data he has filled will automatically be preserved when the Form is submitted i.e. on PostBack with the help of ViewState.
2. Store data which is needed across PostBacks
It can also be used to store data from Code-Behind such as DataTables, XML, etc. in ViewState objects. For example, you can store the name of the Logged-in User in a ViewState so that multiple calls to Database can be avoided.
 
 
Benefits of ViewState
ViewState has the following benefits: -
1. Automatically preserves the data in Controls across PostBacks.
2. Can be used to store small amount of data using ViewState objects.
3. Saves data on Client Side thus reduces the load on the Server.
4. The data is saved in Hashed format and hence it is secure.
 
 
Disadvantages of ViewState
ViewState has the following disadvantages: -
1. ViewState saves all the data in a Hidden Field on the Page itself, thus it increases the size of the Page and also the amount of data being downloaded and sent to the Server thus affecting Bandwidths.
2. Though the data is hashed and hence cannot be decoded, but the data can be modified by Hackers and posted to Server which could result in crashing of the application.
3. Scope is limited to Page level.