WebPart.LoadViewState method

Restores view-state information from a previous request that was saved with the SaveViewState() method.

Namespace:  Microsoft.SharePoint.WebPartPages
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Protected Overrides Sub LoadViewState ( _
    savedState As Object _
)
'Usage
Dim savedState As Object

Me.LoadViewState(savedState)
protected override void LoadViewState(
    Object savedState
)

Parameters

  • savedState
    Type: System.Object

    sAn object that represents the control state to restore.

Remarks

The WebPart class overrides the base LoadViewState() method in order to implement custom logic that determines how a Microsoft SharePoint Foundation Web Part control restores its state. You, in turn, might want to override the WebPart.LoadViewState method in order to implement custom logic of your own. To implement your own logic for persisting and restoring view state in a control, first override the SaveViewState() method and write your own implementation for saving the state of your control. Then you can override the LoadViewState method and write code to load the saved state.

Examples

The following example overrides the LoadViewState method for a Microsoft SharePoint Foundation Web Part control. It creates an Object array to contain the view state information passed in the savedState parameter. Then it calls the base implementation of the LoadViewState method for the first index location of the array. Finally, the example assigns the value stored at the second index location to a persisted string variable in the Web Part.

Protected _persistedString As String = "Default Value"
Protected Overrides Sub LoadViewState(savedState As Object)
   Try
      Dim viewstate() As Object = Nothing
      If Not (savedState Is Nothing) Then
         ' Load state from the array of objects 
         ' that was saved by SaveViewState.
         viewstate = CType(savedState, Object())
         MyBase.LoadViewState(viewstate(0))
         ' Load custom state
         If Not (viewstate(1) Is Nothing) Then
            Me._persistedString = CStr(viewstate(1))
         End If
      End If
   Catch ex As Exception
      ' Handle exceptions
   End Try
End Sub
protected string _persistedString = "Default Value";
protected override void LoadViewState(object savedState) 
{
   try
   {
      object[] viewstate = null;
      if (savedState != null)
      {
         // Load state from the object 
         // that was saved by SaveViewState.
         viewstate = (object[])savedState;
         base.LoadViewState(viewstate[0]);
         // Load custom state
         if (viewstate[1] != null)
            this._persistedString = (string)viewstate[1];
      }
   catch Exception(ex)
   {
      // Handle exceptions
   }
}

See also

Reference

WebPart class

WebPart members

Microsoft.SharePoint.WebPartPages namespace