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 [C#] public override bool EnableViewState {get; set;} [C++] public: __property bool get_EnableViewState(); public: __property void set_EnableViewState(bool); [JScript] 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.
Example
[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" Controls.Add(myFormObj) textBoxObj.Text = "Welcome to .NET" label1.Text = "Enter a name" buttonObj.Text = "ClickMe" AddHandler buttonObj.Click, AddressOf Button_Click myFormObj.Controls.Add(label1) myFormObj.Controls.Add(textBoxObj) myFormObj.Controls.Add(buttonObj) myFormObj.Controls.Add(label2) 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 MyBase.LoadViewState(viewState) 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 [C#] 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"; Controls.Add(myFormObj); textBoxObj.Text = "Welcome to .NET"; label1.Text = "Enter a name"; buttonObj.Text = "ClickMe"; buttonObj.Click += new EventHandler(Button_Click); myFormObj.Controls.Add(label1); myFormObj.Controls.Add(textBoxObj); myFormObj.Controls.Add(buttonObj); myFormObj.Controls.Add(label2); } 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) base.LoadViewState(viewState); } 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(); } }; [C++] public __gc class WebPage : public Page { private: MyForm* myFormObj; Label* label1; Label* label2; TextBox* textBoxObj; Button* buttonObj; public: WebPage() { Page::Init += new System::EventHandler(this, &WebPage::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 = S"post"; Controls->Add(myFormObj); textBoxObj->Text = S"Welcome to .NET"; label1->Text = S"Enter a name"; buttonObj->Text = S"ClickMe"; buttonObj->Click += new EventHandler(this, &WebPage::Button_Click); myFormObj->Controls->Add(label1); myFormObj->Controls->Add(textBoxObj); myFormObj->Controls->Add(buttonObj); myFormObj->Controls->Add(label2); } 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; } protected: void LoadViewState(Object* viewState) { if(viewState != 0) Page::LoadViewState(viewState); } Object* SaveViewState() { ViewState->Item[S"name"] = textBoxObj->Text; return Page::SaveViewState(); } private: 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
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family