Saturday, 21 December 2013

View State IN Dot Net

View State in Dot Net:

Introduction:- It is used to maintain the state of controls during Page post back and if we save any control value or anything in view state we can access those value through out the page whenever it require for that check.

View state information stored in Html Hidden Field.

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.

Coding For View State

protected void page_load(object sender,Event args e)


if (! ispostback)


string str = "welcome to blog";

if(view state["sample text"]==NULL )


view state["sample text"]==str;


protected void btnclick_click (object sender,Event args e)


lblstring.text = viewstate["sample text"].to string();


No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...