In this video
we will try to understand
what is the importance of
Session
and Viewstate in ASP.NET applications
Web applications
like ASP.NET, JSP or Servelets etc
they use http protocol internally
HTTP protocol
is a stateless protocol
What do we mean by http protocol is a stateless protocol then will try to understand what is the importance of viewstate and session variables
Create a simple application called as
SessionAndViewstate
In this page drag and drop a button here
In the behind code we will implement a very simpl
class variable
There the application is running
Everytime we click on this button we should see
the variable values get incremented- 1,2, 3
If we click on this button
If we again click on the button the value should be 2
Everytime when we click on the button it is not incrementing the value
Put some debug points and see that
why this variable is not getting incremented everytime we click on the button
Run this program in a debug mode
_Conter initializes the 0. The page loading is happening
When we click on the button, the counter string value is 0
This time we have initiated the button click event
After the page load event
the button click event is happening
The Counter value is now 1
When we click on the button again it should display 2
And the value is 0, we were expecting it to have value 1
Web applications like ASP.NET or JSP work on
http protocol
Http protocol is a stateless protocol
The user send a request
the server
send him the response
After that server forget the users
Next time if the same user comes
The server treats him as a new request
Everytime we are hitting the button
a request is sent. Everytime the request is counted as new user
This counter variables initializes again and again
The counter page object is created again and again on the server
For every user ASP.NET treats
those users as new users that is why
the value is not stored for every request and response which the users sends
to the server
We were expecting him to increment the count again and again because of this stateless behavior
it not working accordingly
Session variables and Viewstate
helps you to make your ASP.NET application statefully
We will move the value to the session variable called as
My
Counter
= _Counter.
Everytime we hit the button
we will move this current counter value to the MyCounter
EVerytime we come inside the page, ensure
_Counter takes the value
from MyCounter
Session variables are normally objects so we need to ensure we convert
the session variables to
a proper integers value because this _Counters is a integer data type
Whenever we click on the button, ensuring that current value is moved
to a session variable called as MyCounter
By using the session variable we will be able to maintain states between every request and response
Because of session variables
the things have become statefull
Session variables are useful when we want to make
the http stateless protocol statefull
The same thing we can achieve by using Viewstate
Viewstate
and session variables
help you to maintain states between request and response
They
help us to remember states
because http protocol by default by inherently the behavior of http protocol is a
stateless behavior
In this video we were trying to understand what exactly is
the meaning of stateless with prospective of http protocol and second
we introduce our server to session variables and viewstate
which will help us to make our ASP.NET application statement
In the next video we will try to understand difference between Viewstate and Session variables
Many users say that the Viewstate also helps us to maintain states
While session variables also helps us to maintain states
First we will try to understand the difference between Viewstate and Session variables
then we will go in debth
of understanding how session variables work
what are Inproc session, what are outproc session, how to store session variables in SQL server etc
Thank you so much