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:
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.
Benton
Member
215 Points
79 Posts
Question about ViewState
Apr 01, 2010 06:36 PM|LINK
Hi there,
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:
EnableViewState="false" %> <!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" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox runat="server" ID="txtSomeData" EnableViewState="false" /> <asp:Button runat="server" ID="btnPostBack" Text="Post back!" EnableViewState="false" /> </div> </form> </body> </html>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.
ViewState
mbanavige
All-Star
134966 Points
15422 Posts
ASPInsiders
Moderator
MVP
Re: Question about ViewState
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.
ramiramilu
All-Star
95463 Points
14106 Posts
Re: Question about ViewState
Apr 01, 2010 08:09 PM|LINK
Hello,
Read Steven Cheng [MSFT] reply in this thread...
http://www.velocityreviews.com/forums/t88355-enableviewstate-false-question.html
That thread will give you complete overview...
Thanks.
JumpStart