共用方式為


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 註冊。

適用於

另請參閱