Search code examples
asp.netoptimizationviewstate

Optimizing ViewState


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?


Solution

  • There's not a lot I can tell you except "don't put a lot into your ViewState".

    Places I'd look for optimizations:

    • Anything you added to ViewState yourself
    • Large amounts of data bound to data display controls like GridViews, <x>Lists, and Repeaters.

    GridViews are particularly bad about ViewState; 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 huge ViewState. The only way to get around this is to only bind one page at a time to the GridView, but that means you'll have to do data-side pagination which can be just as painful, or to turn off ViewState for the GridView, which means (arguably) useful features like in-line editing are no longer available.

    There's no silver bullet here.