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.

OnInit方法會覆寫來呼叫RegisterRequiresControlState方法Page來表示自訂控制項使用控制項狀態。The 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) {

    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)
          currentIndex = 0
      End Sub
      Protected Overrides Function SaveControlState() As Object
          If currentIndex <> 0 Then
              Return CType(currentIndex, Object)
              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.