Wiz
Wiz

Reputation: 500

Does Viewstate maintain history

Based on an answer from a candidate I have a confusion regarding the functioning of ViewState in an ASP.Net application. According to the candidate If you have a control with some value and you change that value, on postback it sends the initial value as well as the changed value to the server. So if you continue to change values and submit, the whole tree is sent to the server. I tried searching online but couldnt get any facts regarding the same.

If the above description was confusing, here is an example to give more clarity on the claims:
- We have <asp:TextBox ID="txt1" runat="Server" Text="First Value"/>
- After the page loads i enter "Second Value" in txt1 and hit submit and same page loads again (since i have written no logic in the click event)
- Now I enter "Third Value" in txt1and hit submit again.

According to the candidate the first time I hit submit, both "First Value" & "Second Value" are sent to the server and when the page loads after that, it encrypts both those values and puts it in the ViewState so when I hit submit the second time, the encrypted value in viewstate (which has both "First Value" & "Second Value") is submitted along with "Third Value".

I am not from an engineering background and the candidate was and claimed this is what was taught to him in University. I just want to know if there is any amount of truth in those claims. Any help is appreciated.

Upvotes: 1

Views: 202

Answers (1)

to StackOverflow
to StackOverflow

Reputation: 124716

No, that's not true.

On the second submit, "Second Value" will be sent as part of the ViewState hidden field, and "Third Value" as the value of the TextBox.

However, all three values will be visible on the server at different times during the page lifecycle (which may be what's causing the confusion).

  • During the Init phase of the page lifecycle "First Value" will be set (from the page markup).

  • During the LoadViewState phase, this will be replaced by "Second Value" (from the ViewState)

  • During the LoadPostbackData phase, this will be replaced by "Third Value".

On the next Postback (say "Fourth Value"), you'll be able to see "First Value", "Third Value" and "Fourth Value" at different times during the page lifecycle, but "Second Value" will be gone for good.

Upvotes: 4

Related Questions