Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

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 override void LoadViewState(
	Object 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 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
   }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.