Custom Type State Management Example

This example shows how to enable a complex property (a property whose type is a class that itself has properties) to participate in ASP.NET view state by implementing the IStateManager interface. As described in Server Control Properties Example, you can use your control's ViewState property to manage the state of simple properties. However, to provide state management for a collection property or a property that has subproperties, you must implement the property as a read-only property, and as part of the property type you typically implement IStateManager.

The StateManagedAuthor type defined in this example demonstrates how to implement IStateManager. The StateManagedAuthor class is a complex type that itself has subproperties such as FirstName and LastName. The Author property of the BookNew control described in Custom Property State Management Example is of type StateManagedAuthor.

The implementation of the IStateManager interface in StateManagedAuthor is described in the "Code Discussion" section later in this topic.

No code example is currently available or this language may not be supported.

The StateManagedAuthor class illustrates the pattern for implementing the IStateManager interface commonly used by ASP.NET complex types. StateManagedAuthor defines a property named ViewState stored in a private variable of type StateBag and named viewState in C# and viewStateValue in Visual Basic. The ViewState property mimics the ViewState property of the Control class. StateManagedAuthor stores its properties in its ViewState dictionary just as a control stores its simple properties in the Control class's ViewState dictionary.

The IStateManager interface has one property, IsTrackingViewState, and three methods: TrackViewState, SaveViewState, and LoadViewState. The members of the IStateManager interface have the same semantics as the corresponding methods in the Control class.

The IsTrackingViewState property enables a type that implements IStateManager to coordinate with state tracking in the control in which the type is used. The StateManagedAuthor class uses a private Boolean field (isTrackingViewState in C# and isTrackingViewStateValue in Visual Basic) to store this property. In its implementation of TrackViewState, StateManagedAuthor sets isTrackingViewState or isTrackingViewStateValue to true. It also invokes the IStateManager::TrackViewState method of the private viewState or viewStateValue field corresponding to the ViewState property. The TrackViewState method starts the tracking of changes to items stored in the ViewState property. This means that if an item in the ViewState dictionary is set after TrackViewState is invoked on ViewState, the item is automatically marked as modified.

In its implementation of the SaveViewState method, StateManagedAuthor invokes only the corresponding method of its private viewState or viewStateValue field. Similarly, in its implementation of the LoadViewState method, StateManagedAuthor invokes only the corresponding method of its ViewState property. As described in Server Control Properties Example, the StateBag type of the ViewState property implements IStateManager and is a dictionary with state management built into it.

When you use a type that implements IStateManager in your control, you rely on the type to maintain its own state and invoke the type's state management methods from the TrackViewState, SaveViewState, and LoadViewState methods of your control. Thus, the BookNew control described in Custom Property State Management Example invokes the TrackViewState, SaveViewState, and LoadViewState methods of StateManagedAuthor from its own implementation of these methods.

The StateManagedAuthorConverter in the following code listing is the type converter associated with StateManagedAuthor by means of the TypeConverterAttribute attribute. The StateManagedAuthorConverter class enables conversions from the String type to the StateManagedAuthor type and vice versa as described in the Type Converter Example. The StateManagedAuthorConverter is used in this example to print the author's full name in the Render method of the BookNew class.

Note Note

The StateManagedAuthor type does not need a type converter for state management. StateManagedAuthorConverter is provided as an optional utility class that provides string-to-value conversions. When you implement a type converter for a custom type you should override the ToString method of your type to invoke the type converter and return a string representation, as shown in the code listing for StateManagedAuthor.

No code example is currently available or this language may not be supported.

Compile the StateManagedAuthor and StateManagedAuthorConverter classes listed in this topic with the BookNew control listed in Custom Property State Management Example and with the BookType enumeration listed in Server Control Properties Example.

For more information about compiling and using the custom control examples, see Building the Custom Server Control Examples.

Show: