Share via


Visual Basic Concepts

Managing State in DHTML Applications

Typically, Internet applications are stateless, in that the protocol that passes requests and responses between the browser and Web server is not capable of maintaining  information between each request. Therefore, the protocol does not "remember" any information from previous requests when it receives a new request. This is also generally true of DHTML applications, in that the browser does not usually store detailed information between actions.

Because the protocol itself cannot maintain state, you must use other means to store information you want the application to remember between requests. In a DHTML application, you use the GetProperty and PutProperty functions to store and retrieve data. These functions store data as long as the end user's Web browser window is open.

Note   The GetProperty and PutProperty functions are part of the modDHTML module that is added to your project when you create a new project using the DHTML Application template.

You store information using the PutProperty function. In this function, you identify the name of the property in which you want to store information, and the value to store. The browser stores the indicated data in a client-side . The PutProperty function exposes cookie functionality in a way that is simpler to use than directly coding cookies in your event procedures.

You retrieve information using the GetProperty function. In this function, you identify the name of an existing property that you want to retrieve from the client-side cookie. You can assign this value to fields on the page, or use it in your calculations.

Example:  Saving and Retrieving State

For example, suppose you want to build a simple application that will store and retrieve data across page boundaries. The application will consist of two pages: one to store values, and one to retrieve them.

The first page in the application contains a text field into which the end user can enter a value, and a button that stores the value to the cookie. The code for the button, called StoreButton1, would look like this:

Private Function StoreButton1_onclick() As Boolean
   PutProperty BaseWindow.Document, "Property1", TextField1.Value
End Function

The second page in the same application contains a button that, when clicked, retrieves the property from the cookie. The code for that button, called "GetButton1", would look like this:

Private Function GetButton1_onclick() As Boolean
   MyValue.innerText = "The value of the property is " & _
      GetProperty(BaseWindow.Document, "Property1")
End Function

This function uses the innerText property of Dynamic HTML to replace the contents of a DIV tag, called "MyValue," with a sentence which contains the retrieved property.