| By Utpal Chakraborty | Article Rating: |
|
| March 27, 2003 12:00 AM EST | Reads: |
24,730 |
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
First we take an empty page with the following source:
page
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
ViewState can be controlled at four levels the machine level, the
application level, the page level, and the control level. Each level
inherits the setting of the level above by default. For ViewState to be
enabled, all four levels must enable ViewState. By default it is enabled at
each level. If ViewState is disabled at the application level (this is done
in the web.config file) it overrides any page-level setting. Similarly, if
ViewState is disabled at the page level, then it overrides the setting of
any control within the page. A child cannot override the ViewState setting
of the parent. For example, if a page-level ViewState setting is set to
false, controls within the page cannot store ViewState information.
Only controls contained within a
Published March 27, 2003 Reads 24,730
Copyright © 2003 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Utpal Chakraborty
Utpal Chakraborty is a Microsoft Certified Professional. He has extensive experience in design and implementation of n-tier applications and is currently leading many .NET implementations. Before his current experience with .NET he has has man years developing in Java and C++.
![]() |
Dick Haynes 08/10/04 05:41:24 PM EDT | |||
Great Article Thanks |
||||
![]() |
Haresh 06/21/04 03:40:58 AM EDT | |||
Right, But we face problem when we want to do some postback like sorting and paging ( which is essential part of grid). any solution for that...!!! |
||||
![]() |
Vinay Sharma 03/25/04 05:55:44 AM EST | |||
Informative Article |
||||
- iPad3 vs Windows 8 - and the Winner Is...Cloud
- Eleven Reasons Why Windows Phone Will Overtake Android
- Windows Azure Overview Part 4: Security
- Agile Development & Enterprise Architecture Practice – Can They Coexist?
- Eleven Tips for Successful Cloud Computing Adoption
- GM to Pull Facebook Advertising: WSJ
- System Center Virtual Machine Manager 2012 as Private Cloud Enabler
- Apply Agile When Deploying Apps
- The Web – Changing the Way We Work
- EE Times and EDN Announce the 2012 UBM Electronics ACE Award Winners
- Closer Look at One NoSQL Database – MongoDB
- Why Is Scrum So Widely Adopted and So Very Dangerously Deceptive
- iPad3 vs Windows 8 - and the Winner Is...Cloud
- Cisco Unveils Visual Collaboration Solutions in the Post-PC Era, Extending the Reach of TelePresence With New Mobile-to-Immersive Offerings
- Eleven Reasons Why Windows Phone Will Overtake Android
- Windows Azure Overview Part 4: Security
- Agile Development & Enterprise Architecture Practice – Can They Coexist?
- Eleven Tips for Successful Cloud Computing Adoption
- GM to Pull Facebook Advertising: WSJ
- System Center Virtual Machine Manager 2012 as Private Cloud Enabler
- Apply Agile When Deploying Apps
- The Web – Changing the Way We Work
- Book Review: Decision Management Systems
- User Group Malaise?
- Google Maps and ASP.NET
- Converting VB6 to VB.NET, Part I
- How to Write High-Performance C# Code
- Crystal Reports XI & How It Has Changed
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- Programmatically Posting Data to ASP .NET Web Applications
- Implementing Tab Navigation with ASP.NET 2.0
- AJAX World RIA Conference & Expo Kicks Off in New York City
- i-Technology Viewpoint: "SOA Sucks"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"






















