In our last article in the ASP.NET series we discussed Session State Management. This article describes the best practices for View State Management.
View state is used primarily by server controls to retain state only on pages that post data back to themselves. The information is passed to the client and read back in a specific hidden variable called _VIEWSTATE. ASP.NET makes it easy to store any types that are serializable in view state. However, this capability can easily be misused and performance reduced. View state is an unnecessary overhead for pages that do not need it. As the view state grows larger. It affects performance in the following ways:
· Increased CPU cycles are required to serialize and to deserialize the view state.
· Pages take longer to download because they are larger.
· Very large view state can impact the efficiency of garbage collection.
Transmitting a huge amount of view state can significantly affect application performance. The change in performance becomes more marked when your Web clients use slow, dial-up connections. Consider testing for different bandwidth conditions when you work with view state.
Best Practices and Guidelines
You can optimize the way your application uses view state by following the recommendations in the following sections.
Disable View State If You Do Not Need It
View state is turned on in ASP.NET by default. Disable view state if you do not need it. For example, you might not need view state because your page is output-only or because you explicitly reload data for each request. You do not need view state when the following conditions are true:
- Your page does not post back. If the page does not post information back to itself, if the page is only used for output, and if the page does not rely on response processing, you do not need view state.
- You do not handle server control events. If your server controls do not handle events, and if your server controls have no dynamic or data bound property values, or they are set in code on every request, you do not need view state.
- You repopulate controls with every page refresh. If you ignore old data, and if you repopulate the server control each time the page is refreshed, you do not need view state.
There are several ways to disable view state at various levels:
- To disable view state for all applications on a Web server, configure the <pages> element in the Machine.config file as follows. <pages enableViewState=”false” />
This approach allows you to selectively enable view state just for those pages that need it by using the EnableViewState attribute of the @ Page directive.
- To disable view state for a single page, use the @ Page directive: <%@ Page EnableViewState=”false” %>
- To disable view state for a single control on a page, set the EnableViewState property of the control to false, as shown in the following code fragment.
yourControl.EnableViewState = false;
<asp:datagrid EnableViewState=”false” runat= “server” />
Minimize the Number of Objects You Store In View State
As you increase the number of objects you put into view state, the size of your view state dictionary grows, and the processing time required to serialize and to deserialize the objects increases. Use the following guidelines when you put objects into view state:
- View state is optimized for serializing basic types such as strings, integers, and Booleans, and objects such as arrays, ArrayLists, and Hashtables if they contain these basic types. When you want to store a type which is not listed previously, ASP.NET internally tries to use the associated type converter. If it cannot find one, it uses the relatively expensive binary serializer.
- The size of the object is directly proportional to the size of the view state. Avoid storing large objects.
Determine the Size of Your View State
By enabling tracing for the page, you can monitor the view state size for each control. The view state size for each control appears in the leftmost column in the control tree section of the trace output. Use this information as a guide to determine if there are any controls that you can reduce the amount of view state for or if there are controls that you can disable view state for.