Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 839
Danish Khan Avatar asked Jan 28 '11 07:01

Danish Khan


People also ask

What is the purpose of a ViewState?

View state is the method that the ASP.NET page framework uses to preserve page and control values between round trips. When the HTML markup for the page is rendered, the current state of the page and values that must be retained during postback are serialized into base64-encoded strings.

Why is ViewState important when you are working with ASP NET web forms?

The view state is a key element of an ASP.NET page because it is the primary means to persist the state of the Web server controls. Whenever the page posts back, the state is restored, updated using the current form parameters, then used to run the postback event handler.

Which kind of data we can store in ViewState?

Because the data in viewstate is stored as a string, only objects that can be serialized can be stored.. Yes. We can store object in ViewState as it stores the data in the string form although it works like dictionary. Just serialize the object and store the string in ViewState.

What is ViewState property of Web form explain its advantages and disadvantages?

iii) Viewstates are simple. They are used by enabling or disabling the viewstate properties. iv) It is based on the wish of developer that they want to implement it at the page level or at control level. Disadvantages: i) If large amount of data is stored on the page, then page load might cause a problem.


1 Answers

Most obvious reason the ViewState was introduced in ASP.Net was in order to allow a winform-like programming model (based on events).

When you have a server-side control (a textbox for instance), the html page sent back to the browser contains:

  • the initial value of the control, encoded in the viewstate => this value can't be modified by the browser/user
  • the control itself => the browser/user can modify its state

When the form is submitted back to the server, the ASP.Net underlying engine will compare the control new value with the initial value that was stored in the ViewState. If it's not the same, an OnChange event will be triggered, and you can attach to it as you would do in a winform application.

like image 84
yorah Avatar answered Sep 22 '22 19:09

yorah