Changing View State Options

You can specify which option to use at an application level in your application’s Web.config file.  You can also override the application level choice for one or more pages by overriding the GetViewStateLocation() function at the page class.

To change view state options for an application, modify your application’s Web.config to define one of the following keys in the appSettings section.

<appSettings>

     <!-- only use one of the following lines -->

     <add key="ViewStateLocation" value="Page" />

     <add key="ViewStateLocation" value="File" />

     <add key="ViewStateLocation" value="Cache" />

     <add key="ViewStateLocation" value="Session" />

</appSettings>

Changing the Database View State

To change the database view state:

<add key="ViewStateLocation" value="Session" />

 

<sessionstate

     mode="sqlserver"

     cookieless="false"

     timeout="20"

     sqlconnectionstring="data source=MySqlServer;

          user id=ASPState;

          password=1Gr8State"

          server="127.0.0.1"

          port="42424"

/>

Overriding the View State Location for a Specific Page

To override the view state location for a specific page, add the following function to the page class:

Protected Overrides Function GetViewStateLocation() As ViewStateLocation

     ‘ Only use one of the following lines

     Return ViewStateLocation.Page

     Return ViewStateLocation.Cache

     Return ViewStateLocation.Session

     Return ViewStateLocation.File

End Function

Further Reading

ASP.NET Caching Overview:

http://msdn2.microsoft.com/en-us/library/ms178597.aspx