Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
EnableViewState Property

Page.EnableViewState Property

Gets or sets a value indicating whether the page maintains its view state, and the view state of any server controls it contains, when the current page request ends.

[Visual Basic]
Overrides Public Property EnableViewState As Boolean
public override bool EnableViewState {get; set;}
public: __property bool get_EnableViewState();
public: __property void set_EnableViewState(bool);
public override function get EnableViewState() : Boolean;
public override function set EnableViewState(Boolean);

Property Value

true if the page maintains its view state; otherwise, false. The default is true.


[Visual Basic, C#, C++] The following example sets the EnableViewState property to false when the page is loaded. This disables view state for the Page object, meaning that neither view-state information for the page nor any controls contained by the page are saved.

[Visual Basic] 
Public Class WebPage
   Inherits System.Web.UI.Page
   Private myFormObj As MyForm
   Private label1 As Label
   Private label2 As Label
   Private textBoxObj As TextBox
   Private buttonObj As Button
   Public Sub New()
      AddHandler Page.Init, AddressOf Page_Init
   End Sub 'New
   Private Sub Page_Load(sender As Object, e As System.EventArgs)
      ' Comment the following line to maintain page view state.
      Page.EnableViewState = false
      myFormObj.Method = "post"
      textBoxObj.Text = "Welcome to .NET"
      label1.Text = "Enter a name"
      buttonObj.Text = "ClickMe"
      AddHandler buttonObj.Click, AddressOf Button_Click
   End Sub 'Page_Load
   Private Sub Button_Click(sender As Object, e As EventArgs)
      Dim temp As [String] = "<br>Name is " + textBoxObj.Text + "<br>"
      temp += "Saved content of previous page is " + CType(ViewState("name"), String)
      label2.Text = temp
   End Sub 'Button_Click
   Protected Overrides Sub LoadViewState(viewState As Object)
      If Not (viewState Is Nothing) Then
      End If
   End Sub 'LoadViewState
   Protected Overrides Function SaveViewState() As Object
      ViewState("name") = textBoxObj.Text
      Return MyBase.SaveViewState()
   End Function 'SaveViewState
   Private Sub Page_Init(sender As Object, e As EventArgs)
      AddHandler Me.Load, AddressOf Me.Page_Load
      myFormObj = New MyForm()
      label1 = New Label()
      label2 = New Label()
      textBoxObj = New TextBox()
      buttonObj = New Button()
   End Sub 'Page_Init
End Class 'WebPage

public class WebPage : Page
   private MyForm myFormObj;
   private Label label1;
   private Label label2;
   private TextBox textBoxObj;
   private Button buttonObj;

   public WebPage()
      Page.Init += new System.EventHandler(Page_Init);

   private void Page_Load(object sender, System.EventArgs e)
      // Comment the following line to maintain page view state.
      Page.EnableViewState = false;
      myFormObj.Method = "post";
      textBoxObj.Text = "Welcome to .NET";

      label1.Text = "Enter a name";
      buttonObj.Text = "ClickMe";
      buttonObj.Click += new EventHandler(Button_Click);
   private void Button_Click(object sender, EventArgs e)
      String temp = "<br>Name is " + textBoxObj.Text + "<br>";
      temp += "Saved content of previous page is " + ViewState["name"] as String;
      label2.Text = temp;
   protected override void LoadViewState(object viewState)
      if(viewState != null)
   protected override object SaveViewState()
      ViewState["name"] = textBoxObj.Text;
      return base.SaveViewState();
   private void Page_Init(object sender, EventArgs e)
      this.Load += new System.EventHandler(this.Page_Load);

      myFormObj = new MyForm();
      label1 = new Label();
      label2 = new Label();
      textBoxObj = new TextBox();
      buttonObj = new Button();

public __gc class WebPage : public Page
    MyForm* myFormObj;
    Label* label1;
    Label* label2;
    TextBox* textBoxObj;
    Button* buttonObj;

        Page::Init += new System::EventHandler(this, &WebPage::Page_Init);

    void Page_Load(Object* /*sender*/, System::EventArgs* /*e*/)
        // Comment the following line to maintain page view state.
        Page::EnableViewState = false;
        myFormObj->Method = S"post";
        textBoxObj->Text = S"Welcome to .NET";

        label1->Text = S"Enter a name";
        buttonObj->Text = S"ClickMe";
        buttonObj->Click += new EventHandler(this, &WebPage::Button_Click);

    void Button_Click(Object* /*sender*/, EventArgs* /*e*/)
        String* temp = String::Format( S"<br>Name is {0}<br>", textBoxObj->Text );
        temp = String::Concat( temp, S"Saved content of previous page is ", ViewState->Item[S"name"] );
        label2->Text = temp;
    void LoadViewState(Object* viewState)
        if(viewState != 0)

    Object* SaveViewState()
        ViewState->Item[S"name"] = textBoxObj->Text;
        return Page::SaveViewState();
    void Page_Init(Object* /*sender*/, EventArgs* /*e*/)
        this->Load += new System::EventHandler(this, &WebPage::Page_Load);

        myFormObj = new MyForm();
        label1 = new Label();
        label2 = new Label();
        textBoxObj = new TextBox();
        buttonObj = new Button();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Page Class | Page Members | System.Web.UI Namespace

© 2015 Microsoft