Viewstate vs Session
Currently i am working on Web Application for which i had to perform some research on storing values in web application Which one is best to be used, As we all known HTTP Protocol is State less. Page can't hold variable [Value] on post back. So let see the working and difference
Viewstate vs Session 
ViewState
and Session are used to stored value
Syntax  : 
Viewstate(“VariableName”) = “Value” 
 :   Session(“VariableName”) =  “Value” 
1. ViewState are only accessible to page.
                i.e. Page1.aspx has
Viewstate("Var1") Same will not be accessible in Page2.aspx. 
                      Instead once you call
Viewstate("Var1") in page2.aspx it will create new Viewstate 
                      object/Variable for That page
                > Viewstate uses Hidden Field
to store Values which are encoded. Junk characters.
    > Viewstate Values are stored in
Browser, Which can be seen using View Source 
       option of  The browser.
                > Viewstate Value is encoded
using Base 64 type of encoding. It can be easily 
                    decoded using any Of the Viewstate Decoding web sites.                     
                     (http://ignatu.co.uk/ViewStateDecoder.aspx)
                > Critical information should
not be stored in Viewstate
2. Session is accessible across page.
                i.e. Page1.aspx has
Session("Var1") same will be accessible from Page2.aspx
                Code
                                Page1.aspx : Page Load event
                                                Session("Var1")
= "From Page One"
                                Page1.aspx
                                                Response.Write(Session("Var1")")
                                                Output:
- From Page One
                > Session Variable is like
global variable which can be accessed from any page. 
                    Session will
                   Return same value across page until it is
changed.         
                > Session Variable are
created on Server
                > Session Variable uses
Cookie to set and get value on server
                > User based unique
Key is generated on server and are
stored in local Cookies 
                    which is
further Used to access
Session Variable on server
                > Session variable should be used
for Critical information’s. As it’s not visible to 
                    users.
                > If user disables Cookie in browse
the session will not work, it will not throw error but 
                   nothing Can be stored or retrieve from session, To Overcome this issue some 
                    tweak can done in Web.Config file with this session will working using querystring 
                    with
encoded language. To achieve same Disable Cookies on browse and open 
                    Web.Config and make
use of
                SessionState
    Querystring are used to send data to
server i.e Key
                http://localhost:51320/(S(xbcli0u5gya0gk55v5r5yoip))/default.aspx
                Note : The SessionState will
work only under  Tag 
Regards
Switin Kotian
Leading is not about ruling it's about guiding junior's in right direction. 
Comments
Post a Comment