How to minimize viewstate size of a page in asp.net? Please help.
protected override PageStatePersister PageStatePersister
{
get
{
return new SessionPageStatePersister(this);
}
}
add the above code in the code behind of the page that generates large viewstate values. This allows storing the viewstate in the session. Only a key for the viewstate should be added now.
You can turn on compression on the server to minimalize size of data transfered through the network or save viewState to disk and never send it to the client.
You have several options to reduce the ViewState:
Most of the points are highlighted within the other answers. Here's one that might be helpful:
Reduce the number of server controls (e.g. web/html controls) especiall those you do not need. Use simple HTML markups instead.
I've seen too many cases of redundant Table/Row/Cell Web Controls where normal < table >, < tr > and < td > will do.
You cannot minimize the size of the ViewState. It's ASP.NET which serializes/deserializes. Though you could selectively disable ViewState for controls that don't need it.
I chose to save the view state on the server in a database itself and not allow it to be passed in the HTML to the client which bloats the page size. You can extend the HiddenFieldPageStatePersister and work around this. I have written a detailed article around this if you wish ...
http://ashishnangla.com/2011/07/21/reducing-size-of-viewstate-in-asp-net-webforms-by-writing-a-custom-viewstate-provider-pagestatepersister-part-12/