State and View State


Wait a minute. If I am reloading the page from scratch every time with the need to initialize values in the Page_Load event handler, how is it that the two text boxes kept the user-entered values when the page reloaded? We didn't add any code to save and restore those values during initialization.

Here's the story. Although the Page_Load event does give you the opportunity to initialize the page every time the page loads, for most fields the page will remember what was in each field. Remember, ASP.NET is designed so that you think it is running just like a Windows Forms application. You would never be happy if the fields on your Windows Form kept clearing out every time the user clicked a button. You wouldn't be happy if they cleared out in a Web Forms application either.

Because web pages are disconnected from the web server most of the time, each web page needs some way to retain the statethe current property and data settingsof each control between page loads. The Web Forms system does this through a feature called View State. Here's how it works: Each ASP.NET web page includes a hidden static field that includes an encrypted and compressed version of all important state information for the controls. When the user makes changes to each control and triggers some event that returns the page to the web server, it returns with both the embedded View State (built up from the previous construction of the page) and all the current settings for every control. Using this combined information, ASP.NET is able to reconstruct the true client-visible state of every control, and communicate that correctly to you in your server-side event handler code.

When you run an ASP.NET application, use the View Source menu command in Internet Explorer or your favorite browser of the month, and you'll see something like the following:

[View full width]

<input type="hidden" name="__VIEWSTATE" value=" /wEPDwUKMTEyMTc3MTQwNg9kFgICAw9kFgICBw8P FgIeBFRleHQFB05vIERhdGFkZGQME+xLedutk85TvXy9OJd kQF02YA==" />


That's the View State. Don't ask me how it works; I won't tell you (because I don't know). But it's not important to know how it works. It's only important that ASP.NET knows how it works so it can keep your application working like the Windows Forms system it truly isn't.

As you add additional controls to your page, the View State will increase in size. Because all of the web page's content must be transported repeatedly over the Internet, a larger View State results in longer transmission times. It is possible to turn off view state for specific controls using their EnableViewState property. If you don't need a control's value retained from page use to page use, it's a good idea to turn it off.




Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net