I have seen lots of confusion in various threads, that How and Why a textbox persists data even when View State is set to off. Even I was confused earlier but I tried to discover it and I found the root cause, so sharing it to you all.
For that, first let’s see the page life cycle on postback.
Now lets first discuss about the View State, How View State works?
If View State is on for any control, during LoadViewstate, the View State data that got saved last time , gets populated in the control. And in last, the SaveViewState method of every controls that are part of the control hiearchy, gets called and combined View State of all the controls gets base64 enocoded and saved.
So as we know the page is recreated every time page makes a trip to the server, the data persistence is done with the help of viewstate.
Now here the point that are we are going to discuss, even if we set off the View State of some controls like textbox, checkbox etc.. the data persists during postback.
Let’s discuss it in bit detail, whenever a page is submitted or posted back to server, the entire form data is posted to the server as a collection with the request. The collection is in the form of NamedValue collection and this collection has the mapping with uniqueid of the control and the value of the control. You can read the data from the form collection by using the following code snippet
//Reading textbox value from the form collection string textboxvalue = Request.Form[textbox1.UniqueID];
ASP.NET uses this primitive to update the control’s value. ASP.NET uses IPostBackDataHandler for the controls that load the data from the form collection.
Actually all the controls which implement IPostbackdatahandler, implement the method LoadPostData and RaisePostDataChangedEvent. But here the key method is LoadPostData, which returns true if the posted value is changed from earlier value and updates it with posted value, else it returns false. Lets see the sample code here
public virtual bool LoadPostData(string uniqueId, NameValueCollection postedCollection) { //Getting the current value of control String currentValue = this.Text; //Getting the posted value from the form collection String postedValue = postedCollection[uniqueId]; //Checks whether the posted value is changed from the current value, if yes updates it with the posted value and return yes if (currentValue == null || !currentValue.Equals(postedValue)) { this.Text = postedValue; return true; } //else return false return false; }
As from the Page Life Cycle, we can see LoadPostData is called after the LoadViewState, whether viewstate is on or not, it gets populated from the posted data. That’s why the data get persisted even if viewstate is set to off for few controls. Following is the complete list of the controls, those implement IPostBackDataHandler.
- CheckBox
- CheckBoxList
- DropDownList
- HtmlInputCheckBox
- HtmlInputFile
- HtmlInputHidden
- HtmlInputImage
- HtmlInputRadioButton
- HtmlInputText
- HtmlSelect
- HtmlTextArea
- ImageButton
- ListBox
- RadioButtonList
- TextBox
I think, this must have helped many of you overcome from this hazzy picture.
Thanks,
Brij
This is something really very appreciable work.keep it up.
Thanks Mate!!
Good article brij.
Thanks Girijesh!! Glad that you liked it
Nice detective work! I’d been wondering about this
Thanks Claire!!!
Nice work brij.
Thanks Abhijit!!!
I didn’t know about that I guess it almost any control that has end user input that has the same behaviour?
Yes!! You can say in this way also..
Helpful…
I got rejected for a job, for not knowing this .. 😦
you must have written this 2 yrs before.. i would have got a job.. 🙂 kidding..
What can I say!! Anyways, now whosoever have read this post, wont be rejected for the same reason …
Thanks Brij
Thanks Brij, But i am a bit confused about the usage of viewstate now.
If alll the user controls can persist data without viewstate, why do we need to keep viewstate on?. what is the extra work that viewstate does?
This is awesome , really very helpful
Pingback: Request Validation with ASP.NET 4.5 : A deep dive « Brij's arena of .NET
Nice explanation….
Glad you liked it!!
Thanks Brij, But i am a bit confused about the usage of viewstate now.
If alll the user controls can persist data without viewstate, why do we need to keep viewstate on?. what is the extra work that viewstate does?
Please answer above question.
James,
Thanks you liked it.
Regarding to your question, It’s not all user controls that can persist data without ViewState, only which control that implements IPostBackDataHandler, I also listed down all the controls (These controls are mainly input controls).
Having said this, lot of other controls uses view state like Label, GridView and lot many other controls as well. Apart from this we can also directly use the ViewState to store our custom data.
Hope it must have answered your question.
Thanks,
Brij
Thank you much, it helped me to clear my confusion
Glad that it helped you 🙂
What you’ve written makes sense, but I put together a test solution with a Listbox control on it and then populate that Listbox control using javascript. Upon postback, the Listbox control loses its data. I also added a second Listbox controlto the solution and then populate that listbox with data typed into a textbox which is then placed into the listbox by clicking on a button that does postback. This second listbox will retain data on postback which is what your article states. I’ve been trying to figure out how to retain the data on postback for a control populated by javascript, but haven’t been able to figure it out just yet. If you have any advice, that would be great. Thanks.
What you are saying is absolutely correct and expected. if you are adding option using javascript then it would not be maintained during postback. For that you probably need some hidden control to store the options during postback and populate during pageload. An example is here http://www.how-to-asp.net/listbox-losing-values-postback/