Hi Guys today i am going to tell you about view state.
What is a View State in asp.net ?
View state is a mechanism to preserve data across postbacks.
where Viewstate resides ?
view state resides within a page in hidden field with value equals to _VIEW_STATE,and its in encoded string.
whenever you request for a page the web server proceeds the request and send response with aspx page and during this process view state is view state decoded and sent to client's browser.
why to use a view state ?
it want to preserve your information on certain postback,then you need to use viewstate.let me explain how?suppose you bind a drop down with items fetched from database.Now if you do this activity on each time when the page is post back means on each fresh http request then you will slow down your page speed because each time you will interact with db and retrieve all records and bind them.Instead of doing so,just bind the records for the first httprequest and then for all other request use view state of this instead.you can do this as follows:
if (!Page.IsPostBack)
{
//your db stuff here.
}
now if your page is post back again & again you don't need to perform db stuff.
enableviewstate ? in page directive if you set this attribute to true then viewstate will be enabled for whole page,for control specific you can change this to false or true.
What is the role of enableViewStateMac ? well MAC tends to message authentication code,lets understand use of enableviewstateMac with an example,suppose you have filled a form and its asked for ccnumber,if someone has enabled viewstate on this field and you fill the form in a usual way. after filling up form and checkout but someone decoded this viewstate string and fill the form again,now he is having you creditcard number because he decoded the string,now other field are set to his own. now message authentication code compare both of the string and then if the difference found(as in our case) then all the viewstate is replaced with the older one and this attach will not affect the transaction.
Can we allow viewstate to be encrypted ? yes. setting validation to 3DES(Encryption algo.) in machine key(inside system.web element in web.config) you can allow you view state to be encrypted.benefit of this is that this won't be decrypted without decryption key.
an example of machine key
<machine key validation="3DES|SHA1|MD5" decryption key=.... validation key=..... />
validation key is used to validate the view state and determines is view state has been tempered.
- (SHA1 and MD5 are encoding algo. but SHA1 generates long encoded string so more preferable.)
Persistent Machine key : suppose you are in a web farm scenario where apps is maintained on more than one server.suppose there are two server A,B. now user are not aware of whether this response is coming from web server A or B. so when ViewState is decrypted on server A its served to user but when some part of apps served from server B the dynamically generated decryption key/validation key will not decrypt/validate the view state and hence ViewState_Error will be occur. to avoid this error use a persistent machine key.
