Page.RegisterRequiresControlState(Control) Metoda

Definicja

Rejestruje kontrolkę jako kontrolkę, której stan kontroli musi być utrwalone.

public:
 void RegisterRequiresControlState(System::Web::UI::Control ^ control);
public void RegisterRequiresControlState (System.Web.UI.Control control);
member this.RegisterRequiresControlState : System.Web.UI.Control -> unit
Public Sub RegisterRequiresControlState (control As Control)

Parametry

control
Control

Kontrolka do zarejestrowania.

Wyjątki

Kontrolka do zarejestrowania to null.

Metodę RegisterRequiresControlState(Control) można wywołać tylko przed lub podczas PreRender zdarzenia.

Przykłady

Poniższy przykład kodu przedstawia niestandardową kontrolkę serwera wywołującą metodę RegisterRequiresControlState .

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

Niestandardowe kontrolki serwera używające stanu kontroli muszą wywoływać metodę RegisterRequiresControlState dla każdego żądania, ponieważ rejestracja stanu kontroli nie jest przenoszona z żądania do żądania podczas zdarzenia zwrotnego. Zalecane jest, aby rejestracja wystąpiła w zdarzeniu Init .

Dotyczy

Zobacz też