viewstate

  • Thread starter Thread starter rom
  • Start date Start date
R

rom

I saw a "strange" code line somewhere on the web. it looks
like:
ViewState(myKeyWord) = myValue

what am I missing here? what exactly is the viewstate ?is
it different from session?
 
ViewState is on the page. It travels between server and client. Session is
kept on the server. Which one suits you better depends on the particular
case.

From MSDN: A server control's view state is the accumulation of all its
property values. In order to preserve these values across HTTP requests,
ASP.NET server controls use this property, which is an instance of the
StateBag class, to store the property values. The values are then passed as
a variable to an HTML hidden input element when subsequent requests are
processed.

Eliyahu
 
Viewstate is used to maintain state of the web page when you postback to the
current page, where as session is used across the application, that is, you
can create session in one page and access it in other pages of the web
application. so to summarize viewstate is per the page and session is per
the user

HTH
 
You can tack in variables into both the viewstate and the session "arrays".
Watch out for name consistency when you retrieve it or the code will execute
but not work (storing Session["Somevalue"] and accidentally retrieving
Session["Somvalue"] can leave you scratching your head) Either one can be
very handy for small-scale applications. On a larger scale, they can both
affect design and performance.

Using viewstate with too much information (like storing a full dataset in
it) will slow things down as it is transmitted back and forth from the server
every time. Viewstate will already contain all of the context information for
all of the controls on a page so it may affect performance. Since it is
stored on the page (and sent back and forth) it could be vulnerable to
interception/retrieval.

Using session can limit the expandability of the application since it
requires either a session server or a SQL database to maintain sessions over
multiple servers (webfarms, etc.). There are also security concerns with
session-hijacking too but again they are both easy to use for simple,
small-scale, applications.
 
I repeat your words. ":depends on the particular case."
if the the data needs to be saved for a single page between postback then
use viewstate
(U might save server's memory, but u increase network traffic.)

if you need to save data for eg: UserLoginID or equalivent. then use
session.
(Remember this is saved per user, so size should be as small as it can be )

Regards,
Jignesh Desai.
 
Back
Top