Interview Questions & Answer on ASP.NET ViewState

Q : What is ViewState?

Ans : Web forms have very short lifetimes.In ASP.NET, the data that is entered in controls is encoded and stored in a hidden field. This encoded data is then sent with each request and restored to controls in Page_Init. The data in these controls is then available in the Page_Load event.The data that ASP.NET preserves between requests is called the Web form’s view state.

Q : How do you enable or disable a ViewState for a control on the page?

Ans : Every ASP.NET control has a property called EnableViewState. If EnableViewState is set to true ViewState is enabled for the control. If EnableViewState is set to false ViewState is disabled for the control.

Q : How do you enable or disable a ViewState at the page level?

Ans : At the page level you can enable or disable ViewState using EnableViewState property of the page.

Q : What is the name of the hidden form field in which ViewState of the page is saved?

Ans : _ViewState

Q : What are the performance implications of ViewState?

Ans : ViewState is usually good to retain the state of the controls on the webform across postbacks. If you have a huge DataGrid with tons of data being loaded on every page load. It is a good idea to disable the ViewState of the DataGrid for the page to load faster. If the ViewState of a large DataGrid is not disabled, ViewState can easily get very large, on the order of tens of kilobytes. Not only does the __ViewState form field cause slower downloads, but, whenever the user posts back the Web page, the contents of this hidden form field must be posted back in the HTTP request, thereby lengthening the request time, as well.

Q : When does ViewState restoration happens?

Ans : During the Page_Init event

Q : What are the disadvantages of using ViewState?

Ans : 1. On all page visits, during the save view state stage the Page class gathers the collective view state for all of the controls in its control hierarchy and serializes the state to a base-64 encoded string. (This is the string that is emitted in the hidden __ViewState form filed.) Similarly, on postbacks, the load view state stage needs to deserialize the persisted view state data, and update the pertinent controls in the control hierarchy.

2. The __ViewState hidden form field adds extra size to the Web page that the client must download. For some view state-heavy pages, this can be tens of kilobytes of data, which can require several extra seconds (or minutes!) for modem users to download. Also, when posting back, the __ViewState form field must be sent back to the Web server in the HTTP POST headers, thereby increasing the postback request time.

Q : Is ViewState encoded?

Ans :Yes, ViewState is base-64 encoded.

Q : What happens during the Page_Init event?

Ans : The server controls are loaded and initialized from the Web form’s view state. This is the first step in a Web form’s life cycle.

  « Previous    | Next »
     
Leave a Reply
Name (required)
Email (will not be published) (required)
Web Site

List of Top Hospitals in India