Control.SaveControlState 方法


儲存頁面回傳至伺服器以來,所發生的任何伺服器控制項狀態變更。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.


下列程式碼範例會覆寫自訂 ASP.NET 控制項中的 SaveControlState 方法。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 方法以呼叫 Page 上的 RegisterRequiresControlState 方法,以表示自訂控制項使用控制項狀態。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.

使用控制項狀態的自訂控制項必須先在 Page 上呼叫 RegisterRequiresControlState 方法,才能儲存控制項狀態。Custom controls using control state must call the RegisterRequiresControlState method on the Page before saving control state.


儲存控制狀態時,會將 string 物件當做儲存在 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.