views:

152

answers:

1

In one article I was reading on ViewState, I saw a sentence saying that I should not fall into a mistake to believe that the value of a TextBox is stored in ViewState; it is stored in PostBack data.

From here what I understand is when I post back a web form, the input controls values are stored in HTTP Request body. Not in the Viewstate. But as far as I know ViewState values are stored in an hidden field called __VIEWSTATE anyway.

Then does it mean that __VIEVSTATE value is not posted in HTTP POST Request body as a postback data? Sounds nonesense to me.

In another words, basically if I say the ViewState mechanism for such scenerio works like this, am I seeing it right or skipping something:

  1. You enter a value on an empty TextBox and submit the page

  2. The value of text box is posted back inside POST HTTP Request body. Nothing inside __VIEWSTATE at this point from the TextBox

  3. On the server side, the TextBox is created with the default value on OnInit method of the page

  4. The TrackChange property of ViewState is set to true.

  5. The posted back data of TextBox is loaded. Because it is different than the TextBox defalut value(because the user entered something), the ViewState of this text box is marked as DIRTY.

  6. The new value of the textbox is written into __VIEWSTATE hidden field

  7. From now on __VIEWSTATE hiddenfeild contains the last given value of the TextBox

  8. The page is sent to the user's browser having the __VIEWSTATE hidden field. But this time containing the last value entered by user which will be ready to be rendered

Thanks guys!

burak ozdogan

A: 

ViewState is a hidden input on the page that allows the server to maintain state across multiple requests for the same page. ViewState is maintained by the server, so it stays the same unless the server changes it. ViewState is just a way for the server to talk to itself.

In your example, you wonder why the first time you put a value in a TextBox there doesn't seem to be anything in the ViewState. The reason is that the server has not yet put anything there. When the value of your TextBox reaches the server the first time, the server will place the value in ViewState so it can keep track of it across all subsequent requests.

On the subsequent requests, the TextBox value will be encoded into the ViewState and sent back to the server as POST data.

ctford