1. Home
  2. Computing & Technology
  3. Visual Basic

ASP.NET - Learn Programming for the Networked World - Part 4

By Dan Mabbutt, About.com

7 of 8

Maintaining State

A fundamental design principal of all web pages, not something unique to Microsoft, is that they don't maintain "state" between round trips to the server. So even though "PendingOpFlag" is a global variable, it starts fresh every time. We won't take time to describe it now, but this is also a problem with the "Accum" and "PendingOp" variables too.

As another example of something that Visual Studio does for you, this is not a problem with the contents of the display Textbox because Visual Studio takes care of maintaining state for Visual Studio components automatically! Thanks, Visual Studio!

To solve this problem, we have to preserve the state of these three variables between round trips. Visual Studio also provides a way to do this called "session variables". Keep in mind that there are other methods that Visual Studio provides for state management too. Session variables are basically storage locations that are provided on the server that are "persisted" (remembered) between round trips. Consult the official VS.NET documentation for details, but here's how they can be used in our web user control.

First, instead of initializing the variables in a form "_Load" subroutine, we'll use a "Page_Load" subroutine (double click in the main ".aspx" form to generate the default subroutine). The DIM statements and the form "_Load" subroutine can be deleted since they're replaced by the code shown next.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Learn ASP.NET
  6. ASP.NET - Learn Programming for the Networked World - Part 4

©2009 About.com, a part of The New York Times Company.

All rights reserved.