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)

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

Me.LoadViewState(savedState)

Parameters

savedState
Type: System.Object

sAn object that represents the control state to restore.

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.

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
Show: