The VIEWSTATE size is killing our apps and it keeps growing. The .aspx files are bigger than 2.5Mb in some cases.

The developer is on vacation and I just installed a 90 day trial of VS 2008 to test turning form-level VIEWSTATE off then turning it on again to see if the VIEWSTATE gets reduced substantially. It didn't. It went right back to the same size.

When VIEWSTATE was off the page size was 650K. With it off I tested changing one combo field and it worked. The problem is that some pages are complicated with grids and custom user controls, so I am sure that disabling it can possibly affect whether some of these fields work at all. Is this true?

Our server is Win Server 2008 Enterprise.

Any ideas to reduce the size of VIEWSTATE and keep the app running? I need to get this fixed today.

This is an extremely important app that people across the US are using, and any help is greatly appreciated.

Dani AI

Generated

’s test (turning page/form-level VIEWSTATE off with little effect) points to state being rebuilt by child controls or explicit ViewState usage inside custom controls — was right to suspect grids. Quick triage will show whether the bulk is in VIEWSTATE, EVENTVALIDATION, or elsewhere and whether controls are re-enabling state.

Immediate triage steps:

  • Inspect the rendered HTML (View Source or F12) and check the hidden fields named __VIEWSTATE and __EVENTVALIDATION — copy the value into a text editor to see how large each is.
  • Search the codebase for ViewState[...] and for explicit EnableViewState attributes; properties that stash DataTables, Lists or big objects into ViewState are common culprits.
  • For a quick functional test, move page state off the page temporarily by using the session-based PageStatePersister (this removes the large hidden field so the page can be exercised without the payload). Example:
protected override PageStatePersister PageStatePersister
{
    get { return new SessionPageStatePersister(this); }
}

Use this only as a diagnostic or short-term mitigation — it moves payloads into session memory and can explode server RAM under load.

Longer-term fixes and notes:

  • Stop putting full datasets into ViewState. Store only small keys in view state and rebind large data from Cache/Session/DB on postback; implement server-side paging so grids render far less data per request.
  • Recreate dynamic controls during Page_Init so they don’t rely on preserved ViewState. Remember that ControlState remains even when ViewState is off; controls can still require small essential state.
  • If an upgrade path exists, ASP.NET 4+ provides ViewStateMode to disable viewstate by default and enable it only where needed.
  • Consider compression or server-side persistence of page state only after measuring memory/CPU tradeoffs.

Checklist for an urgent fix: locate and remove any ViewState[...] usage that stores full objects, disable viewstate on the largest controls and rebind on postback, test with SessionPageStatePersister to confirm functionality, and monitor session memory and __EVENTVALIDATION size.

Viewstate is used to persist values of controls across postbacks. For example, say you had a form where you fill out name, address, etc. but you forgot your email address. When you submit it the code behind says "need email address" and returns the page so you can fill out the missing value. Without viewstate every box would (without server side coding) be empty. Viewstate makes it easier of the developer in this situation.

Everything should work without viewstate, if it was done right.

I suspect that the grid controls are your problem (along with the custom controls that display a lot of data).

Try turning off viewstate for any gridviews on a page to see if that is what is causing the problem. You do that by adding doing this:

<asp:GridView ID="GridView1" runat="server" EnableViewState="false">

Note the EnableViewState property. You can do that with any custom controls, also.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.