Unit 3 - State Management3
Unit 3 - State Management3
Unit 3 - State Management3
Management
UNIT - 3
State
View State can be used to maintain the State at a page level. The term
"Page Level" means that the information is being stored for a specific
page and until that specific page is active (i.e. the page which is being
currently viewed by the user).
Once the user is re-directed or goes to some other page, the
information stored in the View State gets lost.
It basically makes use of a "Dictionary Object" to store data, which
means that the information is stored in a key and value pair.
It stores this information in a Hidden field on the page itself in a
hashed format.
View State can store a string value only of a specific length. If the
length is exceeded then the excess information is stored in another
hidden field.
View State
You can set View State on/off for each control using EnableViewState
property. By default, EnableViewState property will be set to true.
View state information of all the controls on the page will be
submitted to server on each post back.
To reduce performance penalty, disable View State for all the controls
for which you don't need state. (Data grid usually doesn't need to
maintain state).
You can also disable View State for the entire page by adding
EnableViewState=false to @page directive
// Add item to ViewState
ViewState["myviewstate"] = myValue;
//Reading items from ViewState
Response.Write(ViewState["myviewstate"]);
View State
Advantages
It is very simple to use.
Data is stored in hashed format and hence a layman won't be able to
understand the value of the View State (It still can be hacked by
Hackers, so to make it more secure we should try to store the value in
an encrypted format.).
It is customizable
Disadvantages
Information is not encrypted, so it can be easy for a Hacker to get its
value.
Cannot be used to store sensitive data (eg: Passwords, Credit Card
Pins, etc).
Might make a page heavy if lots of data is stored in View State.
Query strings
Advantages
Simple to Implement
Disadvantages
Human Readable
Client browser limit on URL length
Cross paging functionality makes it redundant
Easily modified by end user
Cookies