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 注册。

适用于

另请参阅