开发者

ViewState lost on refresh in UpdatePanel?

Rather than using the Session object or storing to the database, I am storing temporary variables that I need persisted to custom ViewState variables. For example, ViewState("MyField1") = 1

When the user hits the browser Rrefresh button, Page.IsPostback is back to False and the ViewState is gone.

My question is. If the user can blow away the Viewstate by refreshing, why would anyone use it?

I know that a Refresh reposts the last submitted page, by why is Page.IsPostback reset to False and the ViewState blown away?

Flame me if you want for creating a potential dup question, but I've read other postings here, and it ain't sinking in...

Update to original post:

开发者_StackOverflow社区I now think that it has to do with postbacks that are performed as a result of clicking on Buttons that are within an UpdatePanel. Can someone help shed some light on this?


When a client refreshes their browser, it re-submits the last full page request issued by the client (which may be a GET or a POST). It does not ever resubmit AJAX requests such as those produced by update panel event triggers ("partial page postbacks").

The fact that Page.IsPostback is false when you refresh the page means that your original request is a GET, so here's what's probably happening:

1) During the initial request, the client sends no form data to the server - hence no hidden field containing view state data (Understanding ASP.NET View State is pretty detailed, but a great read if you want to really understand what's going on). While processing this request, ASP.NET may send some view state back to the client, but the original request is just a URL.

2) When the user clicks a button within an UpdatePanel, they trigger a partial postback during which MyField is set to 1. The UpdatePanel changes the client's view state to reflect the new value.

At this point, if the user submits a POST request by normal means, such as clicking a button, the view state will contain the updated information.

If the user clicks 'Refresh' though, they re-submit the original request from step 1, with no form data and therefore no view state.


Where do you set your ViewState? And where do you re-read your ViewState value? Maybe oyu check its content before asp.net calls the LoadViewState() method.


User hitting refresh and using updatepanel will not work together very well. I quess this is why people say that WebForms provides a leaky abstraction on web programming and some are moving to mvc.

If you're not interested in migrating, I'd give you the advice that do not use updatepanel for too long or big operations, where you can assume that user might refresh the page. Use it for small things like dropdown2 items changing when selection on dropdown1 changes.

Wrapping lots of functionality in one updatepanel will cause trouble, if you just depend on viewstate.


Your question is, "Why would anybody use it."

Viewstate comes in handy for data you know is generated by a post back. Hitting refresh is not a post back, but a fresh request.

So lets say you are browsing a datagrid and you need to know certain bits of data about what they have clicked, on the click event you could store that data in the viewstate and process it during other times in the page life cycle, or subsequent post backs.

ViewState's advantage is that it is just embedded into the HTML, so it is all client side. Where as SessionState is server side, and if you store a great amount of data in the session you can cause your web or db server to work harder to handle that data.

Hope this helps.


Don't know why it works but I had a similair problem and solved it by putting this line in the form_load:

me.myProperty = me.myProperty

where

Public Property myProperty() as String
Get
  If Not IsNothing(ViewState("data")) Then
    Return CType(ViewState("data"), String)
  Else
    Return String.Empty
  End If
End Get
Set(value As String)
  ViewState("data") = value
End Set
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜