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.
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.
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With