Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The ASP.NET Framework includes a couple of tools that make it easy to save data persistently between page requests. The ViewState object, which is a part of all ASP.NET web form pages, allows you to place data in an object that's been persisted from one page to another when those pages are posting to each other, and session variables are placed where you can put data that's saved between page requests, regardless of whether the pages are posting or simply redirecting to each other.
For the exercises in this chapter, I'll use a website that's part of the Exercise Files. Go to the Visual Web Developer menu and select File > Open Web Site, locate the Sessions folder under Ch11Sessions and open it, and then open the file FilteredReport.aspx. This is a filtered report page. It includes a DropdownList server control that's populated with data from one SQLDataSource and when the user selects an item from the pull down list, the filter is then applied to another data source, which drives the data that fills a GridView.
I have described how to build this page in the chapter on Reports. I want to use this chapter to describe how the ViewState object works. I'll run the page in the browser. When the page first opens, it retrieves data from the Publishers table on the server and populates the DropdownList. When the user selects an item from the DropdownList, the page is posted back, the filter is reapplied, and the GridView presents the resulting records. The ViewState object is being used dynamically by the ASP.NET Framework. You will notice that when the page reloads after the postback that the DropdownList still shows the item that the user has selected. The .NET Framework is using this object known as the ViewState object automatically.
When you select the item, a property is placed in the ViewState object, and then when the page reloads, it looks in the ViewState and gets the data out. You can actually see the ViewState in your page if you view the page's source in the browser. In the browser I'll right-click on the page ands select View Source. The ViewState object is here. You will see that it's a fairly heavy object that's placed into the page, and then this page right now is storing quite a bit of information. It's storing the name of the page, the data item that the user selected in the DropdownList control and also all of the data that's being presented in the GridView.
This data is all in encrypted in a format called Page 64, it's not readable to the human eye but easy for the .NET Framework to decrypt. The problem with the ViewState object is that it is heavy and it adds quite a bit of size to each web form page, and in many situations, you don't need all that data. You can make your pages quite a bit lighter by disabling the ViewState object and controls that don't really need it. An example in this case would be the GridView. Although you do need ViewState capability in the DropdownList, that is, you want it to retain the user selection between page loads, you don't need to save all the data from the GridView.
Each visual control in the .NET Framework supports a property called Enable ViewState. I'll select the GridView object and then go to the Properties panel, I'll double-click it to make it float, and then resize it and then I'll go to the Enable ViewState property. Notice that it's set to true by default. I'll change its value to false. You won't see anything change in the visual presentation, but now I'll close the Properties view and run the page again. I once again choose an item from the Publisher's pull down list and then look at the source code, and you will see that this time the ViewState data is significantly smaller.
Particularly when you retrieve and present large amounts of data, the ViewState objects makes your web page quite a bit larger, and on a slower network, this can mean that it takes longer for the page to load. One of the ways in which ASP.NET developers tune their websites is to go through the pages and turn the ViewState capability off on those controls that don't really need it. It's also worth mentioning that you can use the ViewState object and add arbitrary properties to the ViewState so that you can pass data between pages, but this capability is limited to the situation where you are using post architecture, that is you are using data entry forms, or the automatic ASP.NET form object, and posting back to the server.
ViewState does not work when you are simply loading multiple pages using what are known as HTTP Get Request. To persist data between those kinds of requests, you will need to learn how to use session variables. And I'll show you how to do that in another video.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98522 Viewers
61 Video lessons · 85812 Viewers
71 Video lessons · 69715 Viewers
56 Video lessons · 102027 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.