Control.LoadControlState(Object) Metoda

Definicja

Przywraca informacje o stanie kontroli z poprzedniego żądania strony, które zostało zapisane przez metodę 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)

Parametry

savedState
Object

Element Object reprezentujący stan kontroli, który ma zostać przywrócony.

Przykłady

Poniższy przykład kodu zastępuje metodę LoadControlState w niestandardowej kontrolce ASP.NET. Po wywołaniu tej metody określa, czy stan kontroli został wcześniej zapisany dla kontrolki, a jeśli tak, ustawia właściwość currentIndex wewnętrzną na zapisaną wartość.

Metoda OnInit jest zastępowana, aby wywołać metodę RegisterRequiresControlState na Page obiekcie , aby wskazać, że kontrolka niestandardowa używa stanu kontrolki.

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

Uwagi

Zastąpij tę metodę, gdy musisz określić sposób przywracania stanu kontroli niestandardowego serwera. Aby uzyskać więcej informacji, zobacz omówienie zarządzania stanem ASP.NET.

Dotyczy

Zobacz też