Click here to Skip to main content
13,894,216 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 6 Feb 2011
Licenced CPOL

Why textbox persists data during postback even if View State set to off

, 6 Feb 2011
Rate this:
Please Sign up or sign in to vote.
Why textbox persists data during postback even if View State set to off

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 I am sharing it with you.
For that, first let’s see the page life cycle on postback.

ASP.NET Page Life Cycle

Now let's 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 control that is part of the control hiearchy, gets called and combined View State of all the controls gets base64 encoded 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 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 a little 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. Let's 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 that 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 this hazy picture.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.

He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.

He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website

He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.

Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.

Visit his Blog: Code Wala

Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008

You may also be interested in...

Comments and Discussions

Questionreally a good article,but i got a question Pin
xcchcaptain9-Aug-13 18:51
memberxcchcaptain9-Aug-13 18:51 
GeneralMy vote of 5 Pin
Thanks787210-Jul-13 18:12
protectorThanks787210-Jul-13 18:12 
GeneralMy vote of 5 Pin
Thanks787229-Apr-13 19:39
protectorThanks787229-Apr-13 19:39 
GeneralMy vote of 5 Pin
Kinzever30-Oct-12 19:58
memberKinzever30-Oct-12 19:58 
GeneralMy vote of 5 Pin
Sandesh M Patil1-Jul-11 23:57
memberSandesh M Patil1-Jul-11 23:57 
GeneralMy vote of 5 Pin
Kunal Chowdhury «IN»26-Feb-11 7:53
mentorKunal Chowdhury «IN»26-Feb-11 7:53 
Generalthanks for sharing - have 5 Pin
Pranay Rana8-Feb-11 1:45
professionalPranay Rana8-Feb-11 1:45 
GeneralRe: thanks for sharing - have 5 Pin
Brij8-Feb-11 6:04
mentorBrij8-Feb-11 6:04 
GeneralThere is an article for the same in CP Pin
Ankur\m/6-Feb-11 23:35
professionalAnkur\m/6-Feb-11 23:35 
GeneralRe: There is an article for the same in CP Pin
Brij7-Feb-11 1:40
mentorBrij7-Feb-11 1:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190306.1 | Last Updated 7 Feb 2011
Article Copyright 2011 by Brij
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid