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 屬性 (Property)。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. 如需有關控制狀態與檢視狀態之間的差異的詳細資訊,請參閱vs 控制項狀態。檢視狀態範例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.