Control.SaveViewState 方法


儲存自頁面回傳至伺服器以來所發生的任何伺服器控制項檢視狀態變更。Saves any server control view-state changes that have occurred since the time the page was posted back to the server.

 virtual System::Object ^ SaveViewState();
protected virtual object SaveViewState ();
abstract member SaveViewState : unit -> obj
override this.SaveViewState : unit -> obj
Protected Overridable Function SaveViewState () As Object


傳回伺服器控制項的目前檢視狀態。Returns the server control's current view state. 如果沒有與控制項相關聯的檢視狀態,則這個方法會傳回 nullIf there is no view state associated with the control, this method returns null.


下列範例會覆寫自訂 ASP.NET 伺服器控制項中的 SaveViewState 方法。The following example overrides the SaveViewState method in a custom ASP.NET server control. 叫用這個方法時,它會決定控制項是否有任何子控制項,以及包含的 Page 物件是否為回傳的結果。When this method is invoked, it determines whether the control has any child controls and whether the containing Page object is the result of a postback. 如果兩者皆為 true,則會將 Label Web 服務器控制項的 Text 屬性變更為 讀取 Custom Control Has Saved StateIf both are true, it changes the Text property of a Label Web server control to read Custom Control Has Saved State. 然後,它會將控制項的檢視狀態儲存為物件的陣列,名為 allStatesIt then saves the view state of the control as an array of objects, named allStates.

protected override object SaveViewState()
{  // Change Text Property of Label when this function is invoked.
   if(HasControls() && (Page.IsPostBack))
      ((Label)(Controls[0])).Text = "Custom Control Has Saved State";
   // Save State as a cumulative array of objects.
   object baseState = base.SaveViewState();
   string userText = UserText;
   string passwordText = PasswordText;
   object[] allStates = new object[3];
   allStates[0] = baseState;
   allStates[1] = userText;
   allStates[2] = PasswordText;
   return allStates;
Protected Overrides Function SaveViewState() As Object
   ' Change Text Property of Label when this function is invoked.
   If HasControls() And Page.IsPostBack Then
      CType(Controls(0), Label).Text = "Custom Control Has Saved State"
   End If
   ' Save State as a cumulative array of objects.
   Dim baseState As Object = MyBase.SaveViewState()
   Dim _userText As String = UserText
   Dim _passwordText As String = PasswordText
   Dim allStates(3) As Object
   allStates(0) = baseState
   allStates(1) = _userText
   allStates(2) = PasswordText
   Return allStates
End Function


「檢視狀態」是伺服器控制項屬性值的累積。View state is the accumulation of the values of a server control's properties. 這些值會自動放在伺服器控制項的 ViewState 屬性中,這是 StateBag 類別的實例。These values are automatically placed in the server control's ViewState property, which is an instance of the StateBag class. 這個屬性的值接著會在伺服器控制項生命週期的 [儲存狀態] 階段之後保存到字串物件中。This property's value is then persisted to a string object after the save state stage of the server control life cycle. 如需詳細資訊,請參閱ASP.NET 網頁生命週期總覽For more information, see ASP.NET Page Life Cycle Overview.

儲存 view 狀態時,會將這個字串物件當做儲存在 HTML HIDDEN 元素中的變數傳回給用戶端。When view state is saved, this string object is returned to the client as a variable that is stored in an HTML HIDDEN element. 當您撰寫具有自訂檢視狀態的自訂伺服器控制項時,可以使用 SaveViewStateLoadViewState 方法來明確管理檢視狀態。When you author a custom server control with a custom view state, the view state can be managed explicitly with the SaveViewState and LoadViewState methods. 如需詳細資訊,請參閱ASP.NET 狀態管理總覽For more information, see ASP.NET State Management Overview. 如需有關如何執行自訂會話狀態提供者的詳細資訊,請參閱執行會話狀態存放區提供者For information on implementing a custom session-state provider, see Implementing a Session-State Store Provider.