Welcome to AspAdvice Sign in | Join | Help

ASP.NET: TextBox and EnableViewState="False"

I've answered the question "I have disabled ViewState for my TextBox, why does TextBox retain its values when posting" so many times at ASP.NET Forums and newsgroups that I wanted to put something about it here too.

Answer:
TextBox does not use ViewState as its only mechanism to retain state over postback. When rendered in HTML browser, TextBox is normal INPUT element from browser standpoint. When form is posted, the value of that INPUT element is posted along with the form. This happens whether you have ViewState enabled or disabled. Note that TextBox implements IPostBackDataHandler interface, so it loads the related, posted value from form post collection in its implementation of IPostBackDataHandler.LoadPostData.

Does TextBox use ViewState at all? Yes, it does. TextBox's Text property is implemented pretty much like this:

public string Text
{
 get
 {
  string text=(string)ViewState["Text"];
  return (text==null)? String.Empty : text;
 }
 set
 {
  ViewState["Text"]=value;
 }
}

As you can see, value of Text property is actually stored to ViewState. So, how does TextBox use ViewState in this case? Well, in IPostBackDataHandler.LoadPostData TextBox uses ViewState as it compares the value of Text property to the value that is posted. As you probably guess, this is the case when TextBox's TextChanged event is raised if these values differ. If ViewState is disabled, Text property returns always String.Empty (it has not been assigned a value yet) and as a result posted value and Text property always differ and TextChanged event is raised every time.

After this, posted value is assigned to the Text property and rendered as the value of TextBox and things start over again. Note that Text property (ViewState collection actually) does keep its value during the same request even if ViewState is disabled. Disabling has impact on ViewState persistence not the functionality of ViewState collection. When ViewState is disabled, ViewState collection (System.Web.UI.StateBag) works similarly to the Context.Items collection. It is as it would be cleared for every request, but its values are valid during the same request.

Sponsor
Published Monday, March 15, 2004 10:44 PM by joteke
Filed under:

Comments

# re: ASP.NET: TextBox and EnableViewState="False"

A way around this is to assign the Text property of the textbox control in your code behind page during the preRender event.
Wednesday, March 23, 2005 5:46 PM by joteke

# re: ASP.NET: TextBox and EnableViewState="False"

Well, it depends on what you want to get around. :-)

Assigning the value on PreRender does still get to the ViewState (viewstate is saved after PreRender stage), and of course is rendered out, so when next postback happens the value is still there (note: getting to the view state still means nothing here).

It's not that from where the value is set in the page lifecycle but when the TextBox posts something with the form post collection, this is kept over postbacks to the end of the world unless TextBox is manually cleared.

Page's postback mechanism works automatically when a value is found from post collection with the TextBox's UniqueID as key in the collection).
Wednesday, March 23, 2005 8:07 PM by joteke

# re: ASP.NET: TextBox and EnableViewState="False"

this is very good article about the ASP.NET textbox control
Wednesday, May 25, 2005 11:58 AM by joteke

# cant extract update value of textbox.

I have a form. At Loading time i place some values in textbox. After loading when i update the contents of textBox and press Submit (or say button that display the contents of textbox) then it doesn't show the updated contents. it shows the old values.
Thursday, June 16, 2005 12:37 PM by joteke

# re: ASP.NET: TextBox and EnableViewState="False"

is this applicable only for textboxes all for all server controls that implement LoadPostBackData? I got a dropdownlist with items specified in html code. thus it retains its selected value no matter of viewstate status. As I understood that viewstate bag just used for making a comparison right? than if I changed the content of letsay a Listbox at server side with viewstate enabled that it should be encountered ? am I right?thnx.
Friday, July 08, 2005 8:46 PM by joteke

# re: ASP.NET: TextBox and EnableViewState="False"

It is applicable to those that post along with the form. CheckBox, RadioButton, TextBox, DropDownList, ListBox for example. Of course say with DDL and LB it is that they post only the selection (and only the value if being precise but that's enough to keep the selection) but other than selected items are of course held in view state.

All these do implement IPostBackDataHandler interface, but I wouldn't say its because of that. They more like implement that interface because of being "form-posting" controls and have the state-keep mechanism kind-of "bolted" into.
Friday, July 08, 2005 9:14 PM by joteke

# re: ASP.NET: TextBox and EnableViewState="False"

I'm new to ASP.Net. I understand all of the above except how to make it stop. I have text boxes and dynamic select box options that keep repopulating themselves with their previous(unwanted) values. How can I prevent this?
Wednesday, August 03, 2005 2:54 PM by joteke

# re: ASP.NET: TextBox and EnableViewState="False"

With TextBox set the Text property explicitly to "" and with DropDownList/ListBox set the SelectedIndex property to -1

e.g

TextBox1.Text =""
DropDownList1.SelectedIndex = -1
Wednesday, August 03, 2005 3:48 PM by joteke

# ASP.NET Textbox control and viewstate

Friday, June 18, 2004 11:52 AM by TrackBack

# TextBox doesn't use EnableViewState?

Sunday, October 10, 2004 12:58 PM by TrackBack

# re: ASP.NET: TextBox and EnableViewState="False"

You linked to this post from http://forums.asp.net/633790/ShowPost.aspx over a year ago. The URL in that post's link is incorrect; it points to http://blogs.aspadvice.com/joteke/archive/2004/03/15/767.aspx (which gives an ASP.NET error) when it should apparently point to http://aspadvice.com/blogs/joteke/archive/2004/03/15/2273.aspx
Thursday, January 26, 2006 4:21 PM by Peter Johnson, ASP Insider

# re: ASP.NET: TextBox and EnableViewState="False"

Peter,

thanks for the heads-up, I edited it. Those links were broken when AspAdvice was upgraded some time ago.

You probably understand that I have posted over 6000 posts there since 2002 (4000 since 2004) so I just can't keep up with them...

Friday, January 27, 2006 1:04 PM by joteke

# re: ASP.NET: TextBox and EnableViewState="False"

i am in that problem . i also want to fix the values of text boxes from right to left
New Comments to this post are disabled