Search code examples
asp.netviewstate

Avoid viewstate to display on website source code


I have performance issue on website, because of too long viewstate in source code. on some pages it's size is more than 15-20kb. Which is increasing the load time on browser.

Is there any way to disable viewstate partially or fully without any harm on other module of website. FYI there is one listview and one form on these example pages. where viewstate is very long.

More Example pages

http://www.pricingindia.in/coupons/ebay-in-coupon-codes-20

http://www.pricingindia.in/coupons/flipkart-coupon-codes-32


Solution

  • You probably want to disable for ViewState for your Page in general, and then only enable ViewState for the controls that need / use it.

    See this MSDN page on the Control.ViewStateMode property, it describes how to set that up:

    To disable view state for a page and to enable it for a specific control on the page, set the EnableViewState property of the page and the control to true, set the ViewStateMode property of the page to Disabled, and set the ViewStateMode property of the control to Enabled.

    You will need to do some testing to see which controls need / use the ViewState in your specific app. But, basically,

    • anything that's static, you can disable the ViewState (Buttons, LinkButtons, etc).
    • Any controls whose state doesn't need to be restored between PostBacks, you can disable ViewState (such as a TextBox in a form that is submitted to the server, and then cleared).
    • Any controls that need to keep their state between PostBacks, you want to enable Viewstate (this would often be databound controls like GridViews / etc).

    Doing this should definitely reduce the load that ViewState is putting on your pages.