From the Blogosphere
Improving ASP.NET Performance - Part 12
View State Management
By: Hovhannes Avoyan
Jul. 26, 2012 09:59 AM
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:
There are several ways to disable view state at various levels:
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.
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:
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.
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
SYS-CON Featured Whitepapers
Most Read This Week