Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

IStateFormatter Interface

Defines methods that a type implements to serialize and deserialize an object graph.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public interface IStateFormatter

The IStateFormatter type exposes the following members.

Public methodDeserializeDeserializes an object state graph from its serialized string form.
Public methodSerializeSerializes ASP.NET Web server control state to string form.

The IStateFormatter interface defines methods that a type can implement to serialize and deserialize the state that an ASP.NET Web server control maintains in its ViewState property. This infrastructure is used by classes that derive from the PageStatePersister class to maintain an ASP.NET page's state between requests. By default, ASP.NET page state is serialized and deserialized by an instance of the ObjectStateFormatter class; however, site and adapter developers can implement the IStateFormatter interface on their own types to perform this work.

For more information about Web server control state management and view state, see ASP.NET State Management Overview and Dynamic Web Server Controls and View State.

The following code example demonstrates how to create a PageStatePersister object that saves view and control state to a stream on the Web server. The StreamPageStatePersister class demonstrates how to override the Load and Save methods to extract and save page state information. These methods use the IStateFormatter interface inherited from the PageStatePersister class to serialize and deserialize view state. This code example is part of a larger example provided for the PageStatePersister class.

namespace Samples.AspNet.CS

    using System;
    using System.IO;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;

    // The StreamPageStatePersister is an example view state
    // persistence mechanism that persists view and control
    // state on the Web server.
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class StreamPageStatePersister : PageStatePersister

        public StreamPageStatePersister(Page page)
            : base(page)
        // Load ViewState and ControlState.
        public override void Load()
            Stream stateStream = GetSecureStream();

            // Read the state string, using the StateFormatter.
            StreamReader reader = new StreamReader(stateStream);

            IStateFormatter formatter = this.StateFormatter;
            string fileContents = reader.ReadToEnd();

            // Deserilize returns the Pair object that is serialized in
            // the Save method.
            Pair statePair = (Pair)formatter.Deserialize(fileContents);

            ViewState = statePair.First;
            ControlState = statePair.Second;
        // Persist any ViewState and ControlState.
        public override void Save()

            if (ViewState != null || ControlState != null)
                if (Page.Session != null)
                    Stream stateStream = GetSecureStream();

                    StreamWriter writer = new StreamWriter(stateStream);

                    IStateFormatter formatter = this.StateFormatter;
                    Pair statePair = new Pair(ViewState, ControlState);

                    // Serialize the statePair object to a string.
                    string serializedState = formatter.Serialize(statePair);

                    throw new InvalidOperationException("Session needed for StreamPageStatePersister.");
        // Return a secure Stream for your environment.
        private Stream GetSecureStream()
            // You must provide the implementation to build
            // a secure Stream for your environment.
            return null;

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2015 Microsoft