UserControl.LoadViewState(Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从用 SaveViewState() 方法保存的上一个用户控件请求还原视图状态信息。
protected:
override void LoadViewState(System::Object ^ savedState);
protected override void LoadViewState (object savedState);
override this.LoadViewState : obj -> unit
Protected Overrides Sub LoadViewState (savedState As Object)
参数
示例
以下示例演示使用 LoadViewState 和 SaveViewState 方法管理其视图状态的用户控件。
public string UserText
{
get
{
return (string)ViewState["usertext"];
}
set
{
ViewState["usertext"] = value;
}
}
public string PasswordText
{
get
{
return (string)ViewState["passwordtext"];
}
set
{
ViewState["passwordtext"] = value;
}
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void LoadViewState(object savedState)
{
object[] totalState = null;
if (savedState != null)
{
totalState = (object[])savedState;
if (totalState.Length != 3)
{
// Throw an appropriate exception.
}
// Load base state.
base.LoadViewState(totalState[0]);
// Load extra information specific to this control.
if (totalState != null && totalState[1] != null && totalState[2] != null)
{
UserText = (string)totalState[1];
PasswordText = (string)totalState[2];
}
}
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override object SaveViewState()
{
object baseState = base.SaveViewState();
object[] totalState = new object[3];
totalState[0] = baseState;
totalState[1] = user.Text;
totalState[2] = password.Text;
return totalState;
}
Public Property UserText() As String
Get
Return CStr(ViewState("usertext"))
End Get
Set(ByVal value As String)
ViewState("usertext") = value
End Set
End Property
Public Property PasswordText() As String
Get
Return CStr(ViewState("passwordtext"))
End Get
Set(ByVal value As String)
ViewState("passwordtext") = value
End Set
End Property
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
Dim totalState As Object() = Nothing
If Not (savedState Is Nothing) Then
totalState = CType(savedState, Object())
If totalState.Length <> 3 Then
' Throw an appropriate exception.
End If
' Load base state.
MyBase.LoadViewState(totalState(0))
' Load extra information specific to this control.
If Not (totalState Is Nothing) AndAlso Not (totalState(1) Is Nothing) AndAlso Not (totalState(2) Is Nothing) Then
UserText = CStr(totalState(1))
PasswordText = CStr(totalState(2))
End If
End If
End Sub
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Function SaveViewState() As Object
Dim baseState As Object = MyBase.SaveViewState()
Dim totalState(2) As Object
totalState(0) = baseState
totalState(1) = user.Text
totalState(2) = password.Text
Return totalState
End Function
End Class
注解
此方法主要由 .NET Framework 基础结构使用,不能直接在代码中使用。 但是,控件开发人员可以重写此方法,以指定自定义服务器控件如何还原其视图状态。 有关详细信息,请参阅 ASP.NET 状态管理概述。
可以将视图状态值加载到字段中,以便以后不必从 Control.ViewState 属性中检索它。 还可以在调用 SaveViewState 之前将 值插入 属性中ViewState,这是使字段在服务器的往返行程中持久保存的有效方法。