Microsoft KB Archive/300505

= How To Use QueryString to Persist User Information Among ASP Pages =

Article ID: 300505

Article Last Modified on 7/2/2004

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q300505





IN THIS TASK
SUMMARY
 * Create an HTML Form to Collect User Information
 * Receive User Information in ASP Page
 * Run the Application
 * Troubleshooting



SUMMARY
There are several ways to maintain state, or persist user information, from page to page in your Active Server Pages (ASP) application. This step-by-step procedure demonstrates how to use the QueryString method of the Request object to accomplish this.

back to the top

Create an HTML Form to Collect User Information
Before you can persist user information from page to page, you need a method in which to initially collect that information. This example uses a simple HTML form to create the method that collects the user's first name and age.

To collect the user's information through an HTML form, follow these steps:   In your favorite HTML editor, create a new ASP page named Form.asp, and paste the following code: User Information Form  Name: &#xa0; Age: &#xa0;   NOTE: This sample code uses the GET method of the Form property. The GET method passes the form information in a QueryString.  Save Form.asp in a directory on your Web server (for example, C:\InetPub\Wwwroot\Form.asp)

back to the top

Receive User Information in ASP Page
This section demonstrates how to receive the information that is passed from the form and print it to the screen. In addition, this code sample creates a dynamic hyperlink that is populated with this information to send it to the next page.

To receive the user information in an ASP page, follow these steps:   In your favorite HTML editor, create a new ASP page name Persistinfo1.asp, and paste the following code: Receive User Information Received information from the QueryString. Your name is: <%=Request.QueryString(&quot;Name&quot;)%> Your age is: <%=Request.QueryString(&quot;Age&quot;)%> <a href=&quot;persistinfo2.asp?<%=Request.QueryString%>&quot;> This link sends your information to the next page</a> NOTE: To persist the user's information from page to page through QueryString, every hyperlink on the page MUST be appended with ?(%=Request.QueryString%), as the preceding code demonstrates. </li> Save Persistinfo1.asp in a directory on your Web server (for example, C:\InetPub\Wwwroot\Persistinfo1.asp).</li>  Create a final ASP page named Persistinfo2.asp, and paste the following code: Receive User Information 2 Received information from the QueryString. Your name is: <%=Request.QueryString(&quot;Name&quot;)%> Your age is: <%=Request.QueryString(&quot;Age&quot;)%> </li></ol>

back to the top

Run the Application

 * 1) In your Web browser, type http://<servername>/form.asp in the Address bar, and then press ENTER.

NOTE: For the ASP application to run on the server, you must run these files from the HTTP location and not from the C:\ location.
 * 1) Type some information, and click Submit.

back to the top

Troubleshooting
Consider the following pitfalls when you persist information through a QueryString:
 * If the user physically types in the Address bar to browse to a page on your Web site, state is lost.
 * If you forget to append the QueryString to a hyperlink URL, state is lost when the user clicks that hyperlink because the QueryString no longer contains the information.
 * The QueryString method is limited to simple data types.
 * The QueryString method can only persist information while the user is still on your Web site. As soon as the user leaves your site, state is lost.
 * The QuerySring method is easily visible to users and thus is not suitable for values that should be hidden.

back to the top

Keywords: kbhowto kbhowtomaster KB300505

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.