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)
Available in Sandboxed Solutions: No

protected override void LoadViewState(
	Object 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 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