UserControl.LoadViewState Method
Restores the view-state information from a previous user control request that was saved by the SaveViewState method.
[Visual Basic] Overrides Protected Sub LoadViewState( _ ByVal savedState As Object _ ) [C#] protected override void LoadViewState( object savedState ); [C++] protected: void LoadViewState( Object* savedState ); [JScript] protected override function LoadViewState( savedState : Object );
Parameters
- savedState
- An Object that represents the user control state to be restored.
Remarks
In general, you do not need to call this method. However, if you store custom information in view state, you could override this method to do so.
For example, 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 base.SaveViewState, which is an effective way to make a field persist across round trips to the server.
Example
[Visual Basic, C#, C++] The following example demonstrates a user control that manages its view state using the LoadViewState and SaveViewState methods.
[Visual Basic] Public ReadOnly Property UserText() As String Get Return CStr(ViewState("usertext")) End Get End Property Public ReadOnly Property PasswordText() As String Get Return CStr(ViewState("passwordtext")) End Get End Property <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _ Protected Overrides Sub LoadViewState(savedState As Object) If Not (savedState Is Nothing) Then MyBase.LoadViewState(savedState) End If End Sub <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _ Protected Overrides Function SaveViewState() As Object ViewState("usertext") = user.Text ViewState("passwordtext") = password.Text Return MyBase.SaveViewState() End Function End Class [C#] public string UserText { get { return (string)ViewState["usertext"]; } } public string PasswordText { get { return (string)ViewState["passwordtext"]; } } [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] protected override void LoadViewState(object savedState) { if (savedState != null) base.LoadViewState(savedState); } [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] protected override object SaveViewState() { ViewState["usertext"] = user.Text; ViewState["passwordtext"] = password.Text; return base.SaveViewState(); } [C++] public: __property String* get_UserText() { return dynamic_cast<String*>(ViewState->Item[S"usertext"]); } __property String* get_PasswordText() { return dynamic_cast<String*>(ViewState->Item[S"passwordtext"]); } protected: [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name=S"FullTrust")] void LoadViewState(Object* savedState) { if (savedState != 0) UserControl::LoadViewState(savedState); } [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name=S"FullTrust")] Object* SaveViewState() { ViewState->Item[S"usertext"] = user->Text; ViewState->Item[S"passwordtext"] = password->Text; return UserControl::SaveViewState(); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
UserControl Class | UserControl Members | System.Web.UI Namespace