When to enable/disable Viewstate

2019-02-16 07:05发布

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?

6条回答
倾城 Initia
2楼-- · 2019-02-16 07:36

_Viewstate can unnecessarily increase the number of bytes that need to be transferred. So unless the data is going to be used the next time , it's a good idea to switch it off.

查看更多
小情绪 Triste *
3楼-- · 2019-02-16 07:37

It's a good practice. Unless you use ViewState values on postbacks, or they are required by some complex control itself it's good idea to save on ViewState as part of what will be sent to the client.

查看更多
forever°为你锁心
4楼-- · 2019-02-16 07:42

You may find the information contained in the "ASP.NET State Management Recommendations" article on MSDN useful for making your decision.

Generally in ASP.NET 2.0 and above disabling the ViewState is less destructive due to the introduction of the Control State for storing informaton required for raising events etc.

查看更多
聊天终结者
5楼-- · 2019-02-16 07:45

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.

查看更多
看我几分像从前
6楼-- · 2019-02-16 07:45

Definately a good idea, nothing worse that a page which a developer is binding a dataGrid in the Page_Load every time but also submitting the viewstate!

It's also a really good idea if you are planning on using the UpdatePanel from the AJAX Extensions, it means you're submitting less during the UpdatePanel request.

(Don't flame for saying that an UpdatePanel can be good :P)

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-02-16 07:46

I think it's good practice. Many ASP.NET devs are unaware that their viewstates add tremendous baggage to the HTML that's being sent to their users' browsers.

查看更多
登录 后发表回答