views:

322

answers:

4

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:

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?

+2  A: 

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.

Jeff Sternal
See update to original post
Velika
Updated my answer in light of the new info.
Jeff Sternal
+1  A: 

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.

burak ozdogan
See update to original post
Velika
I checked it in the Page_Load event of the Web Form
Velika
+1  A: 

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.

Morri
A: 

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.

Clarence Klopfstein
Nope, I knew all that. The question is why would anyone use it if it gets cleared when the user does a refresh. My update to my question explained that it was being cleared due to use ofan Update Panel but I sought an explanation. Thanks, nevertheless :-)
Velika