by Rod
5. March 2010 01:36
As web developers we’ve all had our fair share of view state, while trying to minimize them / storing them in a database or even disabling view state for some controls one by one.
When there’s more View state then we need, we lose a lot in performance. (+ not all search engines like itL)
In previous versions of asp.net, view state was enabled by default on pages therefore we had to disable view state on the controls 1 by 1.
Well guess what??
No more CTRL + C EnableViewState="false" AND CTRL + V 1000xx times.
Now it’s possible to simply disable View state on a whole page and just enable it on the controls we want.
Here’s an example:
My page contains 1 button and 1 label, and here’s my view state :
value="/wEPDwUKLTU2ODgwMzc3NWRkf86Sf4W2JXJIMUDuy2TW5uSqft0zrNjXFhCnQ7cjfZ8="
By clicking the button (set to change the label text), the View state gets even bigger.
Now by disabling the view state on the whole page by setting ViewStateMode attribute to “Disabled” on the page directive:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ViewStateMode="Disabled" %>
My view state stays the same size.
As you might have expected, the label doesn’t preserve its value since its view state is also disabled, so your choice to choose which controls you need to enable view state on.