語言

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 中進行登記。

適用於

另請參閱