Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 332
Adam Avatar asked Dec 03 '09 23:12

Adam


People also ask

What is viewstate in Salesforce?

View state holds the state of Visual force page. The view state of a web page is composed of all the data that's necessary to maintain the state of the controller during server requests (like sending or receiving data).

What is the size of view state in Salesforce?

Salesforce allows Visualforce pages to have a maximum view state size of 170KB. The View State tab shows you which elements on your page are taking up that space. A smaller view state size generally means quicker load times. In order to avoid this error, minimize your pages' view state.


3 Answers

Here are some ideas how you can optimize the size of ViewState transferred over the wire (copied from this answer):

  • Disable ViewState for controls that do not need it (this is the most effective solution). E.g. if you can cache some data on the server, then you can re-bind any databound controls with every request and it's not needed to save everything in ViewState.
  • Turn on HTTP compression on the server (IIS). This reduces the size of the page sent to the client, including the ViewState.
  • Compress the ViewState. This has an additional advantage over HTTP compression: it also reduces the size of PostBacks (data sent back to the server), since the ViewState is always sent back to the server during a PostBack. There are various approaches for this, e.g. as shown in this blog post.
  • Store the ViewState on the server instead of sending it in a hidden field with the page. The easiest way to do this is to use the SesionPageStatePersister, but there are other solutions which store the ViewState to disk instead of using the Session (see here for example).
like image 95
M4N Avatar answered Oct 07 '22 21:10

M4N


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.

like image 41
Randolpho Avatar answered Oct 07 '22 22:10

Randolpho


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

like image 26
Shoeb Lodhi Avatar answered Oct 07 '22 22:10

Shoeb Lodhi