Page.EnableViewState 属性

定义

获取或设置一个值,该值指示当前页请求结束时该页是否保持其视图状态以及它包含的任何服务器控件的视图状态。

public:
 virtual property bool EnableViewState { bool get(); void set(bool value); };
[System.ComponentModel.Browsable(false)]
public override bool EnableViewState { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.EnableViewState : bool with get, set
Public Overrides Property EnableViewState As Boolean

属性值

如果该页保持其视图状态,则为 true;否则为 false。 默认值为 true

属性

示例

下面的代码示例在 EnableViewState 加载页面时将 属性设置为 false 。 这会禁用对象的视图状态 Page ,这意味着既不保存页面的视图状态信息,也不保存页面包含的任何控件。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

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();
   }
};
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
   
   
   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
   
   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
   
   Protected Overrides Sub LoadViewState(viewState As Object)
      If Not (viewState Is Nothing) Then
         MyBase.LoadViewState(viewState)
      End If
   End Sub
   
   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
End Class

注解

有关可能要禁用视图状态的原因的信息,请参阅 Control.EnableViewState

即使 EnableViewStatefalse,页面也可能包含隐藏的视图状态字段,ASP.NET 使用该字段来检测回发。

适用于

另请参阅