I generaly disable viewstate for my ASP.net controls unless I explicitly know I am going to require view state for them. I have found that this can significantly reduce the page size of the HTML generated.
Is this good practice? When should be enabled or disabled?
If you disable ViewState, and a page needs it, the page will no longer work.
View state enables a server control to maintain its state across HTTP requests. View state for a control is enabled if all of the following conditions are met: The EnableViewState property for the page is set to true . The EnableViewState property for the control is set to true .
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.
To accomplish our tasks, we use ViewState a lot, but as we know, it doesn't come free - it has a performance overhead. The ViewState is stored on the page in the form of a hidden variable. Its always advised to use the ViewState as little as possible. We also have other ways to reduce the performance overhead.
Yes it is a very good idea. One could argue that it should have been disabled by default by Microsoft, just like caching.
To see how bad Viewstate is in terms of size increased you can use a tool called Viewstate Analyzer. This is particularly useful when you have an existing application developed with Viewstate enabled.
Another good reason to disable Viewstate is that it is really hard to disable at a later stage, when you have loads of components depending on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With