Does anyone have any ideas or references they could point me to regarding optimizing the viewstate of my ASP .NET application? I don't want to turn it off all together, and the main goal of optimizing it is to speed up performance so I don't want to run an expensive function to recursively disable the viewstate for certain controls because that function would slow down the load time of the page which would defeat the purpose.
Any ideas?
ViewState is a client side state management and becomes part of your Request and Response packets and heavy viewstate can indeed slow down your application performance. One quick option to optimize ViewState performance is to keep it on the Server side and use it only when it is needed. This makes sense as ViewState is never really used on client browser end and is always needed on Server side when you Post-back. You can use a Distributed caching system such as AppFabric or NCache to store your ViewState on server side and this should help improve performance.
I have personally worked with NCache which has a no code change provider for ViewState caching.
Click here to view the article for ASP.NET View State Caching
There's not a lot I can tell you except "don't put a lot into your
ViewState
".Places I'd look for optimizations:
GridViews
,<x>Lists
, andRepeaters
.GridViews
are particularly bad aboutViewState
; everything you databind goes into it, so if you bind a particularly large list expecting ASP.NET to handle pagination of it for you, you're going to have a hugeViewState
. The only way to get around this is to only bind one page at a time to theGridView
, but that means you'll have to do data-side pagination which can be just as painful, or to turn offViewState
for theGridView
, which means (arguably) useful features like in-line editing are no longer available.There's no silver bullet here.
Here are some ideas how you can optimize the size of ViewState transferred over the wire (copied from this answer):