Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 01, 2010 08:09 PM by ramiramilu
Apr 01, 2010 06:36 PM|LINK
I thought disabling ViewState for a server control would cause this control not to keep its state after a postback. I thought the same would happen if I disable ViewState for the whole page.
However, I tried to create an example to show this to a colleague and was puzzled when a textbox on a page would keep is state (in this case its Text property) after one or more postbacks, even when ViewState is disabled everywhere (both at Page level and
Server Control level). Please see the code below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<form id="form1" runat="server">
<asp:TextBox runat="server" ID="txtSomeData" EnableViewState="false" />
<asp:Button runat="server" ID="btnPostBack" Text="Post back!" EnableViewState="false" />
I can keep pressing the Post back! button and every value typed on the textbox is still there after the postback(s). I'm using VS 2008.
After this example I now assume I don't understand ViewState as I thought I was. Can someone please enlighten me?
Thanks in advance.
Apr 01, 2010 07:16 PM|LINK
A TextBox does not use viewstate to preserve the value of the Text property.
Instead, when the form is posted back to the server, the textbox gets the text value that was posted and uses that posted value to restore the text property.
So viewstate is not required or used to retain the Text property.
Apr 01, 2010 08:09 PM|LINK
Read Steven Cheng [MSFT] reply in this thread...
That thread will give you complete overview...