Page.RegisterRequiresControlState(Control) Metoda

Definice

Zaregistruje ovládací prvek jako ovládací prvek, jehož stav ovládacího prvku musí být trvalý.

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

Ovládací prvek, který se má zaregistrovat.

Výjimky

Ovládací prvek pro registraci je null.

Metodu RegisterRequiresControlState(Control) lze volat pouze před událostí nebo během události PreRender .

Příklady

Následující příklad kódu ukazuje vlastní serverový ovládací prvek volající metodu 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

Poznámky

Vlastní serverové ovládací prvky, které používají stav řízení, musí volat metodu RegisterRequiresControlState pro každý požadavek, protože registrace pro stav ovládacího prvku se nepřenese z požadavku na požadavek během události postback. V události se doporučuje provést Init registraci.

Platí pro

Viz také