Export (0) Print
Expand All
Expand Minimize

WebPart.LoadViewState Method

Windows SharePoint Services 3

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
Type: Object. An object that represents the control state to restore.

The WebPart class overrides the base LoadViewState() method in order to implement custom logic for how a Windows Sharepoint Services 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.

A Web Part control’s view state is the accumulation of all its property values. In order to preserve these values across HTTP requests, Web Parts and other server controls automatically place them in the ViewState property for the control. This property is then persisted to a string object and passed as a variable to an HTML hidden input element on the page before it is served to the client. When the page is posted back to the server, the page parses the view state string at page initialization and restores property information in the control.

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 override the LoadViewState method and write code for loading the saved state.

The following example overrides the LoadViewState method for a Windows Sharepoint Services 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
   }
}

Community Additions

ADD
Show:
© 2014 Microsoft