Page.RegisterRequiresControlState(Control) 方法
定义
将控件注册为具有持久性控件状态的控件。Registers a control as one whose control state must be persisted.
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
要注册的控件。The control to register.
例外
要注册的控件为 null。The control to register is null.
RegisterRequiresControlState(Control) 方法只能在 PreRender 事件期间或之前调用。The RegisterRequiresControlState(Control) method can be called only before or during the PreRender event.
示例
下面的代码示例演示了调用方法的自定义服务器控件 RegisterRequiresControlState 。The following code example shows a custom server control calling the RegisterRequiresControlState method.
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 在每个请求上调用方法,因为在回发事件期间,不会将控件状态的注册结转到请求。Custom server controls that use control state must call the RegisterRequiresControlState method on each request because registration for control state is not carried over from request to request during a postback event. 建议在事件中进行注册 Init 。It is recommended that registration occur in the Init event.