Control.EnableViewState Control.EnableViewState Control.EnableViewState Control.EnableViewState Property


获取或设置一个值,该值指示服务器控件是否向发出请求的客户端保持自己的视图状态以及它所包含的任何子控件的视图状态。Gets or sets a value indicating whether the server control persists its view state, and the view state of any child controls it contains, to the requesting client.

 virtual property bool EnableViewState { bool get(); void set(bool value); };
public virtual bool EnableViewState { get; set; }
member this.EnableViewState : bool with get, set
Public Overridable Property EnableViewState As Boolean


如果服务器控件保持其视图状态,则否 true;否则为 falsetrue if the server control maintains its view state; otherwise false. 默认值为 trueThe default is true.


下面的示例将EnableViewState属性设置为。 falseThe following example sets the EnableViewState property to false.

void Page_Load(object sender, System.EventArgs e)
      // Set EnableViewState to false to disable saving of view state 
      // information.
      myControl.EnableViewState = false;
      if (!IsPostBack)
         display.Enabled = false;
Sub Page_Load(sender As Object, e As System.EventArgs)
   ' Set EnableViewState to false to disable saving of view state 
   ' information.
   myControl.EnableViewState = False
   If Not IsPostBack Then
      display.Enabled = False
   End If 
End Sub


视图状态使服务器控件能够跨 HTTP 请求维护其状态。View state enables a server control to maintain its state across HTTP requests. 如果满足以下所有条件,则会启用控件的视图状态:View state for a control is enabled if all of the following conditions are met:

有关更多信息,请参见 ViewStateMode 属性。For more information, see the ViewStateMode property.

服务器控件的视图状态是其所有属性值的累计。A server control's view state is the accumulation of all its property values. 为了跨 HTTP 请求保留这些值,ASP.NET 使用StateBag类的实例来存储属性值。In order to preserve these values across HTTP requests, ASP.NET uses an instance of the StateBag class to store the property values. 然后,在处理后续请求时,这些值将作为变量传递给隐藏的字段。The values are then passed as a variable to a hidden field when subsequent requests are processed. 有关视图状态的详细信息,请参阅ASP.NET 视图状态概述For more information about view state, see ASP.NET View State Overview.

在某些情况下,需要禁用视图状态,尤其是为了提高应用程序性能。There are times when it is appropriate to disable view state, particularly to improve application performance. 例如,如果要将数据库请求加载到服务器控件中,请将此属性设置为falseFor example, if you are loading a database request into a server control, set this property to false. 如果不这样做,则会浪费处理器时间,将视图状态加载到仅被数据库查询重写的服务器控件中。If you do not, processor time will be wasted loading view state into the server control that will only be overridden by the database query. 如果EnableViewStatefalse,则可以使用控件状态保存特定于控件的属性信息,并且不能像视图状态属性那样关闭。If EnableViewState is false, you can use the control state to persist property information that is specific to a control and cannot be turned off like the view state property. 有关控件状态和视图状态之间的差异的详细信息,请参阅控件状态与视图状态示例For more information on the difference between control state and view state, see Control State vs. View State Example.

有关如何以声明方式为 ASP.NET 页启用或禁用视图状态的信息,请参阅 @页For information about how to enable or disable view state declaratively for an ASP.NET page, see @ Page.