Page.RegisterRequiresControlState(Control) Metod

Definition

Registrerar en kontroll som en kontroll vars kontrolltillstånd måste bevaras.

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)

Parametrar

control
Control

Kontrollen som ska registreras.

Undantag

Kontrollen som ska registreras är null.

Metoden RegisterRequiresControlState(Control) kan bara anropas före eller under händelsen PreRender .

Exempel

I följande kodexempel visas en anpassad serverkontroll som anropar RegisterRequiresControlState metoden.

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

Kommentarer

Anpassade serverkontroller som använder kontrolltillstånd måste anropa RegisterRequiresControlState metoden för varje begäran eftersom registreringen för kontrolltillstånd inte överförs från begäran till begäran under en postback-händelse. Vi rekommenderar att registreringen sker i händelsen Init .

Gäller för

Se även