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. 然后,它将控件的视图状态另存为一个名为 allStates的对象数组。It 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.

保存视图状态时,此字符串对象将作为存储在 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.