ASP.NET ViewState related Interview Questions

What is ViewState?
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.

How do you enable or disable a ViewState for a control on the page?
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.

How do you enable or disable a ViewState at the page level?
At the page level you can enable or disable ViewState using EnableViewState property of the page.

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

What are the performance implications of ViewState?
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.

When does ViewState restoration happens?
During the Page_Init event

What are the disadvantages of using ViewState?
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.

Is ViewState encoded?
Yes, ViewState is base-64 encoded.

Can you encrypt ViewState of Page?
Yes, we can use the LosFormatter class to encrypt ViewState of Page

Can the HTML controls retian State accross postbacks?
No, by default HTML controls donot retain state accross postbacks.

Can you make HTML controls retain State accross postbacks?
Yes, HTML controls can retain State accross postbacks, if you convert HTML controls to Server Controls. There are 2 ways to convert HTML control to Server Controls.

1. Right click on the HTML Control and then click "Run As Server Control"
2. Set runat="server" attribute for the Control.

Is ViewState supported in classic ASP?
No,ViewState is introduced in, it was not in classic asp.

When a form is submitted in classic ASP, all form values are cleared. Suppose you have submitted a form with a lot of information and the server comes back with an error. You will have to go back to the form and correct the information. You click the back button, and what happens.......ALL form values are CLEARED, and you will have to start all over again! The site did not maintain your ViewState.

When a form is submitted in ASP .NET, the form reappears in the browser window together with all form values. How come? This is because ASP .NET maintains your ViewState. The ViewState indicates the status of the page when submitted to the server.

Is ViewState of one page available to another page?
No, ViewState of a Page is available only in that page. You cannot access ViewState of one page from another page.

Can you programatically store and retrieve data from ViewState?
Yes. In ASP.NET you can programatically store and retrieve data from ViewState.See the example below

//Save the value in ViewState object
ViewState("SomeVar") = txtFirstName.text;

//Retrieve the value from ViewState object
String strFirstName = ViewState("SomeVar").ToString();

Can someone view the Page HTML source and read ViewState?
No. ViewState is base-64 encoded. Hence you cannot read ViewState. If you right click on the Page and View Source you will find __ViewState is base-64 encoded.

What happens during the Page_Init event?
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.


  1. hi thx alot for such a wonderful questions and answers...thxxxxxxxxxxxxxxxxxxxx

  2. this one was brilliant .. thanks...

  3. this not only gives good questions and answers, but explains it neatly

  4. Best questions and answers on Viewstate.

  5. Can you make HTML controls retain State accross postbacks?
    Yes, HTML controls can retain State accross postbacks, if you convert HTML controls to Server Controls. There are 2 ways to convert HTML control to Server Controls.

    1. Right click on the HTML Control and then click "Run As Server Control"
    2. Set runat="server" attribute for the Control.

    I tried both the options. The first option was not available and second option did not work.

  6. I got this error: 'ViewState' is a 'type' but is used like a 'variable' when i tried this in ASP.NET C#

    ViewState("SomeVar") = txtFirstName.text;
    String strFirstName = ViewState("SomeVar").ToString();

    This one worked fine.
    ViewState["SomeVar"] = tbxName.Text;
    lblName.Text = ViewState["SomeVar"].ToString();

  7. @Merin, the first format you used is for ASP.NET/VB.NET. You cannot use that syntax in ASP.NET/C#. That's why in C# when you tried the second format, it worked. Because that't the right syntax for C#. Your first one will work in ASP.NET with VB.

  8. You Done A Great Job......I done my whole assignment from your website.....Great Work.......

  9. Great Work. Thank you so much

  10. Hi All,

    I need some clarifications regarding viewstate concept in

    I have taken one textbox & button control and I set the enableviewstate=false for the textbox control. In the button click event i wrote the following code:


    But i'm not understanding how the enableviewstate=false property is affecting the application.
    I would really appreciate if anyone can explain me the below queries

    1. How the "enable view state=false" works for a control,for a page and the application.
    2. I didn't find the hidden field where it is.
    3. I heard that viewstate will do encrypting. But how it does i didn't understand.

    If possible provide me simple examples.



  11. Is viewstate can be hacked .
    Why because it encoded instead encrypted.

  12. Hi there,
    I just created a sample web site. Added all the Page Events, ViewState is not available on Page_Init event.
    It is available at Page_PreLoad Event.

    Can you please re confirm the same?

    between this article helped me a lot. ManyThanks


If you are aware of any other questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

Disclaimer - Terms of use - Contact Us