What does the "EnableViewState" property do? Why would I want it on or off?

Showing Answers 1 - 5 of 5 Answers

Jatinder Singh Rana

  • Aug 8th, 2005
 

In Dot Net State of Objects/Controls is stored in hidden field named _ViewState this is done automatically by dot net  
life time of this control is life of page (i.e till we are doing PostBack to same page) 
through EnableViewState=true /false we can define whether we have to maintain values of control in hidden fields when page is posted back again and again

  Was this answer useful?  Yes

soumya

  • Sep 21st, 2005
 

set EnableViewState on to avoid data loss after round trip to the server

Naresh

  • Oct 6th, 2005
 

soumya Wrote: set EnableViewState on to avoid data loss after round trip to the server

By setting EnableViewState off for the controls whose data we can afford to loss will make the PostBack cycle faster

  Was this answer useful?  Yes

prashanthdd

  • Apr 28th, 2008
 

It enables the page to store the user inputs on a form across postback.It saves the server side values for a given control into viewstate which is stored as a hidden value in the form before sending the page to the client browser.When the page is posted back to the server the server control is recreated with the state stored in the viewstate.

  Was this answer useful?  Yes

mrp100461

  • Aug 6th, 2009
 

EnableViewState is enable is a property on controls that allows that control to save to the hidden _viewstate.  the control itself holds it value in the controls ViewState property, if  EnableViewState is set to true. You store the values in ViewState
VB.net
' Add property values to view state with set;
' retrieve them from view state with get.

Public Property [Text]() As String
    Get
        Dim o As Object = ViewState("Text")
        If (IsNothing(o)) Then
            Return String.Empty
        Else
            Return CStr(o)
        End If
    End Get
    Set(ByVal value As String)
        ViewState("Text") = value
    End Set
End Property
C#
private const int defaultFontSize = 3;

// Add property values to view state with set;
// retrieve them from view state with get.
public String Text
{
    get
    {
        object o = ViewState["text"];
        return (o == null)? String.Empty : (string)o;
    }

    set
    {
        ViewState["Text"] = value;
    }
}
However if you are using session variables you can use these instead of viewstate that is when EnableViewState is set to false

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions