Control.SaveViewState Control.SaveViewState Control.SaveViewState Control.SaveViewState Method

定義

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

protected:
 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.

範例

下列範例會覆寫SaveViewState自訂 ASP.NET 伺服器控制項中的方法。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,它會變更Text的屬性LabelWeb 伺服器控制項,讀取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.

儲存檢視狀態時,此字串物件會傳回給用戶端做此變數會儲存在 HTMLHIDDEN項目。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.

適用於

另請參閱