Control.EnableViewState 屬性


取得或設定值,該值表示伺服器控制項是否對要求的用戶端而言保持其檢視狀態,以及它包含的任何子控制項狀態。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. 如需 view 狀態的詳細資訊,請參閱ASP.NET View State 總覽For more information about view state, see ASP.NET View State Overview.

有時候,您可以停用 view 狀態,特別是為了改善應用程式效能。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. 如果不這麼做,處理器時間就會浪費在將 view 狀態載入到伺服器控制項中,只會由資料庫查詢覆寫。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. 如果 falseEnableViewState,您可以使用控制項狀態來保存控制項特定的屬性資訊,而不能像 view state 屬性一樣關閉。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.