Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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?

like image 981
Chad Avatar asked Jul 12 '26 04:07

Chad


2 Answers

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.

like image 168
Jeff Sternal Avatar answered Jul 14 '26 19:07

Jeff Sternal


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.

like image 27
pencilCake Avatar answered Jul 14 '26 19:07

pencilCake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!