Search code examples
c#asp.netrequestviewstate

Why is ViewState required when the Request object would have all the field values in it?


Whenever we submit a form, all the field values are posted to the server and are available within the Request object. Ideally, one can use the same object to read the values and perform any of the operations with it.

Then, why do we need ViewState to hold the values of the fields?

Please pardon my ignorance, I think I am missing out sometime pretty obvious here, but cant figure out what.


Solution

  • As per V4Vendetta, you can disable ViewState on your controls, but then you would need to develop manually "ASP classic style" e.g. if you needed to keep the user on the page and if a form failed validation, you would then need to set the initial values back to whatever the user last set them at, otherwise the user data would be lost.

    The ASP Net controls handle this somewhat more elegantly by retaining this information in ViewState. But if you either never need to render the control again, or if you render the control data from fresh every time without needing its previous state (e.g. a paged grid), then disabling ViewState for the control will save you bandwidth.

    A good explanation of ViewState here.