Control.SaveControlState Control.SaveControlState Control.SaveControlState Control.SaveControlState Method

定义

保存将页面回发到服务器之后发生的所有服务器控件状态更改。Saves any server control state changes that have occurred since the time the page was posted back to the server.

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

返回

返回服务器控件的当前状态。Returns the server control's current state. 如果没有与该控件关联的状态,则此方法返回 nullIf there is no state associated with the control, this method returns null.

示例

下面的代码示例将重SaveControlState写自定义 ASP.NET 控件中的方法。The following code example overrides the SaveControlState method in a custom ASP.NET control. 调用此方法时,它将确定内部属性currentIndex是否设置为非默认值,如果是,则将该值保存到控件状态。When this method is invoked, it determines whether the internal property currentIndex is set to a non-default value and, if so, saves the value to control state.

重写RegisterRequiresControlState方法以对调用方法Page ,以指示自定义控件使用控件状态。 OnInitThe OnInit method is overridden to call the RegisterRequiresControlState method on the Page to indicate that the custom control uses control state.

public class Sample : Control {
    private int currentIndex = 0;
   
    protected override void OnInit(EventArgs e) {
        Page.RegisterRequiresControlState(this);
        base.OnInit(e);
    }

    protected override object SaveControlState() {
        return currentIndex != 0 ? (object)currentIndex : null;
    }

    protected override void LoadControlState(object state) {
        if (state != null) {
            currentIndex = (int)state;
        }
    }
}
Class Sample
  Inherits Control
  
  Dim currentIndex As Integer
  
      Protected Overrides Sub OnInit(ByVal e As EventArgs)
          Page.RegisterRequiresControlState(Me)
          currentIndex = 0
          MyBase.OnInit(e)
      End Sub
  
      Protected Overrides Function SaveControlState() As Object
          If currentIndex <> 0 Then
              Return CType(currentIndex, Object)
          Else
              Return Nothing
          End If
      End Function
  
      Protected Overrides Sub LoadControlState(ByVal state As Object)
          If (state <> Nothing) Then
              currentIndex = CType(state, Integer)
          End If
      End Sub
  
End Class

注解

SaveControlState使用方法可保存特定控件操作所需的状态信息。Use the SaveControlState method to save state information required for the operation of a specific control. 此控件状态数据与控件的视图状态数据分开存储。This control-state data is stored separately from the control's view-state data.

使用控件状态的RegisterRequiresControlState Page自定义控件必须在保存控件状态之前对调用方法。Custom controls using control state must call the RegisterRequiresControlState method on the Page before saving control state.

继承者说明

保存控件状态时,字符串对象将作为存储在 HTML HIDDEN元素中的变量返回给客户端。When control state is saved, a string object is returned to the client as a variable that is stored in an HTML HIDDEN element. 重写此方法以提取要在控件中使用的状态信息。Override this method to extract the state information to use in your control.

控件状态适用于少量的关键数据,如页索引或关键字。Control state is intended for small amounts of critical data, such as a page index or a keyword. 对于大量数据,使用控件状态会对页性能产生不利影响。Using control state for large amounts of data can adversely affect page performance. 有关详细信息,请参阅ASP.NET 状态管理概述For more information, see ASP.NET State Management Overview.

适用于

另请参阅