Freigeben über


Visual Basic Concepts

Moving State Between the Browser and the Web Server

A simple way of maintaining state information involves making the information a part of the requests and responses you send between the browser and the Web server. There are three ways to do this:

  • Using to store information on the client and send it to the Web server in requests.

  • Using the webclass's URLData property to move information between the client and the server without permanently storing it in either location.

  • You can use hidden fields to store small pieces of data between the client and the server.

Each approach has its advantages and disadvantages. Cookies can sometimes allow you to send more information than URLData or hidden fields, but some browsers may not allow cookie functionality. URLData ties information to a specific page, which can be useful. Hidden fields bind information to a specific form on a specific page, and are best when you want to send a small amount of data.

Passing State Information with Cookies

You can use cookies to maintain some state information within a session. A cookie is a small packet of information that the server sends to the browser and which the browser stores for subsequent transactions with that server. The cookie includes information about the current user and a range of URLs for which the state is valid. When the browser makes another requests within that range, it includes the cookie in the request data. This allows you to maintain information about server requests and transport it between the client and the server.

Most frequently, cookies are used to store a database key on the client that the server then uses to retrieve state information. In this scenario, you write the database key to the cookie and pass it to the client, where it is stored. The next time the browser sends a request to the same Web site, it includes the cookie. The Web site then uses the cookie value to retrieve the appropriate information from a database table.

There are limits to the number of cookies you can send and the number that can be issued by a single site. It is recommended that you use a small number of cookies in your application and don't use them to send large amounts of data. Due to their size limitations, cookies are ideally suited for sending small pieces of information such as the user's ID.

Note   Remember that users can turn off the ability to support cookies in their browsers, and that cookies can be temporary or not supported at all.

Cookies store information for a specific server, rather than for a specific page in your application. If you want to assign unique state information for a particular page, use the URLData property to manage your state information. See "Passing State Information with the URLData Property," below, for more information.

For More Information   See the HTML reference of your choice for more information on using cookies.

Passing State Information with the URLData Property

You can use the URLData property to transfer information between the browser and the Web server. The URLData property appends information to specific URLs that the webclass sends to the browser. When the browser submits another request using one of these URLs, the information can be passed back to the Web server for further processing. In this way, you can send and retrieve state information without storing it in either location.

The URLData property offers several advantages:

  • It can be used with browsers that do not support cookies.

  • It stores state data on the page itself. Therefore, if the user uses the back button and resubmits the page, the webclass receives the same state data that was originally sent with the page.

There are two main disadvantages to URLData. First, URLData is restricted in the amount of data it can send. The size limitation varies from browser to browser, but most browsers can handle about 2K of data in the URL. You should test the URL length you are planning to use in your application. Second, URLData is not a feasible way of sending information if you are using a form with the in Internet Explorer 4.x. Use the in this situation, or use another means to transmit your state.

The URLData property can add information to URLs in the webclass's responses in two cases:

  • When the webclass calls a WriteTemplate method.

  • When the webclass calls a URLFor method.

In the WriteTemplate scenario, the webclass generates a response that typically involves formatting a template file to send to the browser. The URLData property tells the webclass to perform additional processing on the template — it must append the specified information to each URL in the template file that contains a parameter called WCU.

Note   The webclass adds the WCU parameter to your template each time you connect a tag attribute to a webitem or event. You can also add this notation manually if you are working without a template.

This method of state management is particularly useful when you have selected wcNoState as the StateManagement property setting for your webclass. When wcNoState is selected, you cannot store information within the WebClass object on the server.

Setting State Information for a URLData Response

To set state information to use with URLData, you simply assign a value to the URLData property in your event procedure. Processing differs depending on whether or not you are using a template:

  • If you are working without a template, using Response.Write to send HTML to the browser, the webclass's URLFor method returns a URL to the browser that contains the URLData property's value as a parameter.

  • If you are using a template file, the webclass processes adds your URLData property value to any location in the template where the WCU parameter appears.

Note   When the webclass assigns the URL data to the WCU parameters, it scans the file for all occurances of &WCU and ?WCU, and assigns the value of the URLData property to them in the format &WCU=Your URL data. If, for some reason, you want the letters ?WCU or &WCU to appear as part of your template's text and you want the webclass to ignore this text when it parses the file, enter the text as ?WCUWCU or &WCUWCU. When the webclass runtime finds such as string, it will strip out the extra WCU and leave the text as you intended. This extra level of encoding is only necessary if you assign a value to URLData.

For example, the following code shows how you might set state data within a custom event called "Set" for a webitem called "Item2":

Private Sub Item2_Set()
   'Set the value of the URLData property.
   URLData = "CustomerID: 77788"

   'Use the URLFor function to launch a response.
   Response.Write "<A HREF=""" & URLFor(Item3) & """>Go to Item 3</A>"

End Sub

Retrieving State Information from a URLData Request

If you have previously set the URLData property for a response, the information you sent to the browser will be returned to you when the webclass processes the URL that contains the data. You can retrieve the value of the URLData property in order to manipulate and process the state information on the Web server.

For example, the following code shows how you might retrieve state data within the Respond event for a webitem called "Item2":

Private Sub Item2_Respond()

   'Retrieve state information and use it to customize a response.
   Response.Write "Welcome back," & _
    FetchNameFromDatabase(WebClass1.URLData)

End Sub

For More Information   See "Handling Sequencing in Webclasses" for information on using the URLData property to set a navigational sequence in your application. See "URLData Property" in the Language Reference.

Using Hidden Fields

You can use the HTML HiddenField control on one of your Web pages and use that field to store information for the current page. This method of state management only works on HTML pages that contain a form.

Hidden fields are good for maintaining state for specific forms and are easily accessible from cookies or other client-side scripts. The main disadvantages of using hidden fields are that the information they transmit is bound to a form and is available in the HTML source view for the page, so anyone who can access the HTML source can view the data. You may want to encrypt your data when using this method.

For More Information   See the HTML reference of your choice for more information on using hidden fields.