Share via


Supporting Private View State

In addition to the application view state, you can use automatic services to maintain private state information for an ASP.NET mobile control. When a mobile control maintains internal information, it is recommended that you do not depend on application settings to persist such information; instead, write the application so that it uses a private view state. Examples of private view state include the currently active form on a page, pagination information about a form, and device-specific decisions made by a control's adapter.

A private view state differs from an application view state in a number of ways. It is written out with the rendered page, rather than saved in session state. It also cannot be disabled by the application. And because all private states are dynamically generated, the tracking semantics used for an application view state do not apply to a private view state.

To use a private view state, a control must override the LoadPrivateViewState and SavePrivateViewState methods. These work the same way as the LoadViewState and SaveViewState methods. Do not write your controls in such a manner that they use application view state mechanisms, such as the ViewState state property, to save settings that are part of a private view state.

Control adapters can also participate in a private view state. A control adapter can implement the LoadAdapterState and SaveAdapterState methods. These are called from the MobileControl base class implementation of the LoadPrivateViewState and SavePrivateViewState methods, respectively.

Because a private view state is written out to the client and is not under application control, your mobile controls must use a private view state as efficiently as possible. The following example illustrates one technique for optimizing a control's implementation.

[C#]

protected override Object SavePrivateViewState()
{
    Object baseState = base.SavePrivateViewState;
    Object myState = GetMyState();

    if (baseState == null && myState == null)
        return null;
    else if (myState == null)
        return baseState;
    else
        return new Object[] { baseState, myState };
}

protected override void LoadPrivateViewState(Object state)
{
    if (state is Object[])
    {
        Object[] arr = (Object [])state;
        base.LoadPrivateViewState(arr[0]);
        LoadMyState(arr[1]);
    }
    else if (state != null)
        base.LoadPrivateViewState(state);
}

If a control does not have any private view state to save, return a null reference (Nothing in Visual Basic) from the SavePrivateViewState method. Also, you do not need to save properties that are currently set to their default values with a private view state.

See Also

Supporting View State