Share via


Page.RegisterRequiresControlState(Control) Metode

Definisi

Mendaftarkan kontrol sebagai kontrol yang status kontrolnya harus dipertahankan.

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)

Parameter

control
Control

Kontrol untuk mendaftar.

Pengecualian

Kontrol untuk mendaftar adalah null.

Metode RegisterRequiresControlState(Control) ini hanya dapat dipanggil sebelum atau selama PreRender peristiwa.

Contoh

Contoh kode berikut menunjukkan kontrol server kustom yang RegisterRequiresControlState memanggil metode .

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

Keterangan

Kontrol server kustom yang menggunakan status kontrol harus memanggil RegisterRequiresControlState metode pada setiap permintaan karena pendaftaran untuk status kontrol tidak dibawa dari permintaan ke permintaan selama peristiwa postback. Disarankan agar pendaftaran terjadi jika terjadi Init .

Berlaku untuk

Lihat juga