Page.RegisterRequiresControlState(Control) 메서드

정의

컨트롤을 상태를 지속시켜야 하는 컨트롤로 등록합니다.

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)

매개 변수

control
Control

등록할 컨트롤입니다.

예외

등록할 컨트롤이 null인 경우

RegisterRequiresControlState(Control) 이벤트가 수행되는 동안이나 수행되기 전에만 PreRender 메서드를 호출할 수 있는 경우

예제

다음 코드 예제에서는 호출 하는 사용자 지정 서버 컨트롤을 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

설명

제어 상태에 대한 등록은 포스트백 이벤트 중에 요청에서 요청으로 이월되지 않으므로 제어 상태를 사용하는 사용자 지정 서버 컨트롤은 각 요청에서 메서드를 호출 RegisterRequiresControlState 해야 합니다. 이벤트에 등록 Init 하는 것이 좋습니다.

적용 대상

추가 정보