Does viewstate expire?

2019-01-14 21:06发布

Let's say you have a aspx page that does not rely on session, but does rely on viewstate for persistance between postbacks.

If a user is accessing this page, and leaves for a long lunch, will viewstate still be valid when he returns?

12条回答
做自己的国王
2楼-- · 2019-01-14 21:43

No Viewstate doesnot expires.After redirecting to other page then value of view state lost or expire viewstate. for more detail http://www.c-sharpcorner.com/UploadFile/78d182/Asp-Net-state-management-techniques/

查看更多
太酷不给撩
3楼-- · 2019-01-14 21:48

The short answer is: no.

The longer answer is: it depends on implementation of ViewState storage. You can provide custom implementation of ViewState which could expire after given amount of time. For example, you could store ViewState in database or on disk and send only some reference to the stored value in a hidden field. Then you can use batch processing to remove outdated ViewState data or perform expiration upon request.

查看更多
可以哭但决不认输i
4楼-- · 2019-01-14 21:51

The ViewState will persist from POST to POST. It's actually stored inside a hidden field on your form so that it gets POSTed back to your server all the time.

As long as you aren't relying on the Session you shouldn't have any problems rebuilding page state. It's easy to test your Page's state code if you want though: just set your session to expire after 60 seconds in your web.config then load your page, wait a little more than a minute (surf over to Stack Overflow and answer some questions) and then click a button on your page.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-14 21:53

Sorry to relive this old thread, but new information is available now:

Yes, ViewStates expire. I come from 19 hours researching about a problem of ViewStates losing its values between long time interval postbacks. It took me a while reading MSDN documents and Stackoverflow answers saying it was basically impossible to happen unless a custom ViewState storage implementation was employed, which, now I know, it's not true.

My problem was taking place in a SharePoint 2013 environment. The service known as Distributed Cache (a.k.a. AppFabric) does the caching of the ViewState and has a Time to Live associated to it. You can find more information here: http://blogs.msdn.com/b/besidethepoint/archive/2013/03/27/appfabric-caching-and-sharepoint-1.aspx

The interesting bit of information can be found in this phrase: "To improve page performance, beginning in SharePoint 2013 SharePoint caches ViewState data server-side rather than transferring it back and forth to clients."

I hope this information helps somebody so desperate as I was 19 hours ago.

查看更多
对你真心纯属浪费
6楼-- · 2019-01-14 21:54

Viewstate itself does not expire. Since it's posted back in a form, it can be reconstituted any time.

According to MSDN: "...it is possible for view state to expire if a page is not posted back within the session expiration time". So, in a round about sort of way, it can expire if your session does, but viewstate does not directly expire. Since you're not using session state anyway, you don't have to worry about implicit expiration.

Note that I wouldn't have said it expired. That was MS who I quoted in their own article entitled Controlling ViewState

查看更多
Explosion°爆炸
7楼-- · 2019-01-14 21:54

Also, as a gotcha, by default ASP.NET encrypts ViewState with an autogenerated key. This can be overridden with the MachineKey element in the web.congif file. Even though ViewState won't expire, it can become invalid if a different autogenerated key is used to decrypt ViewState, such as after an IIS Reset, redeploying an application, or hitting a different server in a web farm. If you're planning on storing viewstate for long periods of time, watch out for how it's encrypted/decrypted.

http://msdn.microsoft.com/en-us/library/ms998288.aspx

查看更多
登录 后发表回答