共用方式為


Control.LoadControlState(Object) 方法

定義

SaveControlState() 方法所儲存的上一頁要求中,還原控制項狀態資訊。

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

參數

savedState
Object

Object,代表所要還原的控制項狀態。

範例

下列程式代碼範例會 LoadControlState 覆寫自定義 ASP.NET 控件中的方法。 叫用這個方法時,它會判斷控件之前是否儲存控件狀態,如果是的話,請將內部屬性 currentIndex 設定為儲存的值。

系統會 OnInit 覆寫 方法,以呼叫 RegisterRequiresControlState 上的 Page 方法,以指出自定義控件使用控件狀態。

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

備註

當您需要指定自定義伺服器控制項如何還原其控制項狀態時,請覆寫這個方法。 如需詳細資訊,請參閱 ASP.NET 狀態管理概觀

適用於

另請參閱