|
Comments
|
Today's Top SOA Links
ASP.NET Streamline Performance
Streamline Performance
Mar. 27, 2003 12:00 AM
Like many features available in Microsoft's products, page ViewState in ASP.NET works behind the scenes by default. However, unlike most other features, ViewState can impact the pages we develop dramatically. The impact may not only be in page size but also in server-side performance. Pages with large ViewState can throw unexpected errors. Developers must understand what factors affect page ViewState and the strategies to follow to gain maximum benefit from this feature. Since there is no one strategy for all scenarios, we will review two different scenarios. First, you must understand the benefits of ViewState.The Web is a stateless scenario. In the ASP world, developers often use hidden form fields to store values to persist information across postbacks. Automatic state management is a feature that enables server controls to repopulate their values on a round-trip without requiring you to write any code. However, this feature is not without cost, because the state of a control is passed to and from the server in a hidden form field. Each control defines what it needs to store in its ViewState. They are stored as key-value pairs, using the System.Web.UI.StateBag object. On page postback this data is sent back to the server and ASP.NET uses it to construct the state of the controls on the page during page initialization. To better appreciate the amount of data stored in the ViewState we'll look at two examples.
Examples of Pages with ViewState
If you save it as an .aspx file, open it in a browser, and look at the page source you will see the code shown in Listing 1. (All of the code for this article can be downloaded from www.sys-con.com/dotnet/sourcec.cfm.) There is already some ViewState data because some of the ViewState data is actually ViewState metadata that always exists. This metadata tells ASP.NET how the ViewState data will be applied to create the state of the different page controls. Now, let's consider a page that has a control. The simple page shown in Listing 2 uses a DataGrid with all default settings to display just five rows from a table. On saving this .aspx page and opening it up in a browser we can take a look at the page source, which is shown in Listing 3. That is about 4K of ViewState for just five rows. In a typical scenario in which there will be more controls on a page (and probably a higher number of result rows), the ViewState can be really large. It is thus imperative that we gain some understanding of how to control the size of the ViewState.
Controlling ViewState Only controls contained within a Reader Feedback: Page 1 of 1
Your Feedback
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 |
||||||||||||||||||||||||||||||||||||