UserControl.LoadViewState(Object) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Restores the view-state information from a previous user control request that was saved by the SaveViewState() method.
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)
Parameters
Examples
The following example demonstrates a user control that manages its view state using the LoadViewState and SaveViewState methods.
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
Remarks
This method is used primarily by the .NET Framework infrastructure and is not intended to be used directly from your code. However, control developers can override this method to specify how a custom server control restores its view state. For more information, see ASP.NET State Management Overview.
You can load a view-state value into a field so that you do not have to retrieve it from the Control.ViewState property later. You can also insert the value into the ViewState property just before calling SaveViewState , which is an effective way to make a field persist across round trips to the server.