State Management means to preserve the State of a Control, Web Page, Data, Object or User in application explicitly because all ASP.NET Web Applications are Stateless that is bydefault for each page posted to the Server, the State of Controls is lost.
For Example when a form is submitted in ASP, all form values are cleared,Suppose you have submitted a form with a lot of information and the server comes back with an error , you will have to go back to the form and correct the information ,when you click the Back button and what happens , all form values are cleared and you will have to start all over again. The website did not maintain state of controls , so here we need to store the page values at the time of PostBack we can do it by State Mananagement. Now all Web Applications demand a high level of State Management from control to Application level.
There are two types of State Management techniques listed below:
Client Side State Management : In this technique we manage state of controls at client side following are some
Client Side State Management techniques.
Server Side State Management Techniques: In this technique state of controls or users manage at server side following
are some state management techniques
View State Management
View State is one of the most useful Client Side State Management technique. It can store the page values at the time of Post Back of page(Sending and receiving informtion from/to Server). ASP.Net pages provides the View State Property as a built in structure for automaically storing values between multiple request from the same page.
when a form is submitted in ASP, all form values are cleared,Suppose you have submitted a form with a lot of information and the server comes back with an error ,
you will have to go back to the form and correct the information ,when you click the Back button and what
happens , all form values are cleared and you will have to start all over again. The website did not maintain
state of controls , so here we need to store the page values at the time of PostBack we can do it by View State Property of Controls .
Client Side State Management does not use any Server Resources, it store information using client side options.
Advantages of View State : following are some advantages of View State.
Disdvantages of View State : following are some Disadvantages of View State.
When we should use View State ?
We should use View State
When we should avoid View State ?
We should avoid View State
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 contains information about page and its controls. It does not have any interaction with Server.It stays
alongwith the page in the Client Browser. View State use Hidden Field to store its information in a encoded
format.
You can see view state values by right click on browser and click view source , you will get the
following section of code:-
input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="2NvUlofSaheYe5BlK1m4Yh65gVDCn2o9F722HH3XvSMqUJWOzcs6F17MOkQ22rlkXxXff/2Ig="
Now look at the value, it look likes a encrypted string, this is Base64 encoded string so it can easily be decoded.
Base64 makes a string suitable for HTTP transfer plus it makes it little hard to read.
View["xyz"]= 0; Stored 0 in xyz //means it stores a variable.
View["xyz"]="hello this is Sudhir"; // means it stores the string value.
View[_classObject]=_objectofaClass; //means it stores the object of a Class
How you can store Object in View State ?
We can store an Object easily as we can store String or Integer type variables in View State, we need to Convert it
into Stream of Bytes because View State store information in Hidden field in the page , so we need to use Serialization.
If Object which are tying to store in View State are not Serializable then we will get an error message.
[Serializable]
Public Class ClassName
{
//Class members goes here
}
How to trace View State Information : if you want to trace your View State information , by just enable "Trace" option of page directive.
Page Title="" Language="C#" MasterPageFile="~/Sitemaster.Master" Trace="true"
TraceMode="SortByTime" AutoEventWireup="true"
CodeBehind="frmTokenGeneration.aspx.cs" Inherits="DigitalToken.Staff.frmTokenGeneration"
Now Run your web application , you can view the details of View State size alongwith Control ID in control tree section.
Enable and Disable View State : We can enable and disable view state for a single Control as well as at Page level also. to turn off View State for a single Control , set EnableViewState property of Control to false.
TextBox1.EnableViewState=false;
To turnoff the View State of entire page , we need to set EnableViewState to false of page Directive as shown below
Page language="C#" EnableViewState=false;
Session State Management in ASP.NET
A Session is one of the most useful Server Side State Management technique used for store information of web pages.
A Session is a Unique Instance of the browser. A Single user can have multiple sessions , by visiting your application
with multiple instances of the browser running with a different Session ID on his machine.
Session is Single User Global data.
Session provides facility to store information on Server Memory.It can support any type of Object to store alongwith
our own custom objects.
For every Client , Session data is Stored seperately , which means session data is stored on a per Client basis.
As we all know ,HTTP is a Stateless Protocol , it can not hold client information on a page , if user insert some information
and move to the next page, that data will be lost and the user would not be able to retrieve that information, so need
to store information Session provides these facilities to us.
State Management using Session is one of the best
ASP.NET feature because it is Secure, Transparent from Users and we can Store any kind of Object in it.
Advantages of Session : Following are some advantages of Session-
Disdvantages of Session : Following are some Disadvantages of Session,
Session Events: There are two types of Session Events avialble in ASP.NET.
About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the
applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server,
EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.