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)


Protected Overrides Sub LoadViewState ( _
    savedState As Object _
Dim savedState As Object

protected override void LoadViewState(
    Object savedState


  • 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)
      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())
         ' 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) 
      object[] viewstate = null;
      if (savedState != null)
         // Load state from the object 
         // that was saved by SaveViewState.
         viewstate = (object[])savedState;
         // Load custom state
         if (viewstate[1] != null)
            this._persistedString = (string)viewstate[1];
   catch Exception(ex)
      // Handle exceptions

See also


WebPart class

WebPart members

Microsoft.SharePoint.WebPartPages namespace