Aracılığıyla paylaş


Supporting Private View State

In addition to application view state, you can 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 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.

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

To use 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 to use application view state mechanisms, such as the ViewState state property, for saving settings that are part of private view state.

Control adapters can also participate in 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 private view state is written to the client and is not under application control, your mobile controls must use private view state as efficiently as possible. The following example illustrates one technique for optimizing a control's implementation.

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 null from the SavePrivateViewState method. Note also that you do not need to use private view state to save properties that are set to their default values.

See Also

Other Resources

Supporting View State