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.

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

適用於

另請參閱