Control.LoadControlState(Object) 方法

定义

SaveControlState() 方法保存的上一个页请求还原控件状态信息。

protected public:
 virtual void LoadControlState(System::Object ^ savedState);
protected internal virtual void LoadControlState (object savedState);
abstract member LoadControlState : obj -> unit
override this.LoadControlState : obj -> unit
Protected Friend Overridable Sub LoadControlState (savedState As Object)

参数

savedState
Object

表示要还原的控件状态的 Object

示例

下面的代码示例替代 LoadControlState 自定义 ASP.NET 控件中的 方法。 调用此方法时,它将确定以前是否为控件保存了控件状态,如果是,请将内部属性 currentIndex 设置为保存的值。

OnInit重写 方法以对 调用 RegisterRequiresControlState 方法Page,以指示自定义控件使用控件状态。

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

注解

当需要指定自定义服务器控件还原其控件状态的方式时,请重写此方法。 有关详细信息,请参阅 ASP.NET 状态管理概述

适用于

另请参阅