Overview
State management using a view state is a mechanism provided by the ASP.NET framework to save page elements, and web control data values between postbacks to the server. When a view state is used to persist some aspects of an ASP.NET page, the current state of that page, along with its controls is converted into a string and saved as a hidden field. When the page is posted back to the server, like for instance when a postback event is fired because a server-side control gets clicked, the view state travels back with the page in its hidden field. When the page gets posted again to the browser, the browser parses the page's view state string as it initializes the page, and restores all of the page's properties, and web control data.
Advantages vs Disadvantages
Advantage of State Management
State management can be a powerful ally in situations where a user accesses a chart with a large, relatively static data set. This type of chart usually requires a considerable amount of time to get populated with data. Once populated however, it probably will not require that the data be reloaded again. State management allows you to persist your chart's data between postbacks to the server, which means that in this case, your chart will not require that its data be reloaded each time it is sent back and forth between the server and client. By reducing the necessity to reload data which is likely to remain unchanged, your web applications will not need to access valuable resources, like databases, or other network server data stores. State management using view state allows you to provide a consistent, efficient, and smooth interaction between your chart web applications and end-users.
Disadvantage of State Management
One drawback to state management is that it increases the amount of data that gets sent back and forth from server to client. The increase in the amount of data being transferred is in relative proportion to what is being persisted in the view state. There is a difference between persisting appearance properties, which would require a relatively small increase in the amount of data transferred, and persisting a 1000 data point values, which would require a substantial increase in the amount of data transferred. Generally speaking, you can reduce the necessary over-head, inherent with the use of view states, by persisting only the most necessary chart control data between postbacks to the server.
Using State Management
In order to maintain view state, you must do two things at the client:
- First, you must enable view state by setting the boolean EnableViewState property to True. By default, it is set to False.
- Secondly, you must specify the view state data that you want to persist by using either the ViewStateContent, or ViewStateData properties. The ViewStateContent provides a simple way to specify that a chart element be persisted, and the view state is automatically managed for you. The ViewStateData property on the other hand, represents the actual view state data itself. When you use this property, you are responsible for saving and loading your chart's data when a postback occurs.
Using ViewStateContent
The ViewStateContent property, which defines the data to be saved in the view state, has three possible values including: Default, Appearance, and Data. The default value is SerializationContent.Default, which results in all non-default chart properties of series, and their data point values being saved. The SerializationContent.Appearance value setting results in only appearance-related chart properties being saved, and the SerializationContent.Data value setting results in only the series and their data points being saved.
Note |
---|
In order to reduce the overhead necessary to maintain your chart's state, only those properties that have non-default values will get persisted. |
Example
This example demonstrates how to enable state management, and use the view state to persist a chart series and its data points.
Visual Basic | Copy Code |
---|---|
|
C# | Copy Code |
---|---|
|
Using ViewStateData
The ViewStateData property represents the actual view state data that gets stored at the client. When implementing a user-defined view state, you are responsible for saving and loading all of the view state data. Typically, the approach is to save any data that you want to persist before posting data to the server, then use the view state data when a postback occurs.
Example
This example demonstrates how to save and load the background color of the Chart object.
Visual Basic | Copy Code |
---|---|
|
C# | Copy Code |
---|---|
|
Using the Save and Load Methods
It is also possible to use the Save and Load methods of the Serializer object to save and load the view state data into the control when a postback occurs. To set the view state use the Save method, of the Serializer object, to serialize the chart data to some object derived from the TextWriter class (e.g. StringWriter ). Then set the ViewStateData property using the TextWriter.Write method, as shown in the example code below.
To read data that is persisted using the view state, create a TextReader object, then use the Chart.ViewState property as the string parameter for its constructor. Once this is done, you can load the data using the Load method as shown in the example below.
For more information on serialization, refer to the topic on Serialization.
Example
This example demonstrates how to save a user-defined view state consisting of the chart's appearance properties. Notice that we explicitly save the persisted data, and load it immediately after the postback occurs. Typically, this type of code resides in the Page_Load event handler function. however it can be placed anywhere that it is required.
Visual Basic | Copy Code |
---|---|
|
C# | Copy Code |
---|---|
|
How To
Creating Multiple Charts