View State Improvements in ASP.Net 4.0

Hi friends,

Today, in this discussion, i would like to stress upon View State improvements which is done in ASP.Net 4.0 series. So, it says basically, that view state can be disabled completely @ page level, but it can be enabled @ control level. so, it means you can disable the same @ top level and re-enable the same control level selectively. Initially, it was like disable view state or enable view state, means all or nothing.

so, let’s get started, let’s explain the same visual studio itself.

1st

so, here i have added 2 things, a simple label just to track the no of page loads and one table just to make the view state size huge.

2nd

 

so, when i do every time postback, it will increment the value by 1. so, viewstate is basically a mechanism for remembering the control state.

3rd 4th 5th 6th

 

so, as you see upon submitting that counter gets incremented, but when i saw the page source, i could see the real mess, here the thing, is on every subsequent request viewstate of the page is getting incremented and hence will cause page to load slow. this chunk is basically base 64 encoding which is basically view state encoding mechanism. so, basically what we used to do @ 1st attempt to disable the viewstate @ the page level likewise

7th

 

and that would certainly solve the problem as this will disable completely viewstate from the page and page gets optimized, bang we achieved to the solution. but, we certainly didn’t want this. we wanted to keep track of no of clicks, how would we achieve then, hmmm if you try doing enableViewstate=”True” @ control level, that would not work. so, what we would do instead, we would use ViewStateMode property as shown below.

8th

now, this will solve the problem. Now, new view state will look like this

9th

so, with this i would like to wrap, till then stay tuned and happy coding.

Thanks,
Rahul