How To Use QueryString to Persist User Information Among ASP Pages (300505)



The information in this article applies to:

  • Microsoft Active Server Pages

This article was previously published under Q300505

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:
  1. In your favorite HTML editor, create a new ASP page named Form.asp, and paste the following code:
    <html>
    <head>
    <title>User Information Form</title>
    </head>
    <body>
    <form method="GET" name="User_Info" action="persistinfo1.asp">
     <p>Name: &#xa0;<input name="Name" size="20"></p>
     <p>Age: &#xa0;<input name="Age" size="20" ></p>
     <p><input type="submit" value="Submit"></p>
     <input type="reset" value="Reset">
    </form>
    </body>
    </html>
    						
    NOTE: This sample code uses the GET method of the Form property. The GET method passes the form information in a QueryString.
  2. 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:
  1. In your favorite HTML editor, create a new ASP page name Persistinfo1.asp, and paste the following code:
    <html> 
    <head> 
    <title>Receive User Information</title> 
    </head> 
    <body> 
    <p>Received information from the QueryString. </p> 
    <p>Your name is: <%=Request.QueryString("Name")%></p> 
    <p>Your age is: <%=Request.QueryString("Age")%></p> 
    <p><a href="persistinfo2.asp?<%=Request.QueryString%>">
       This link sends your information to the next page</a></p> 
    </body> 
    </html>
    						
    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.
  2. Save Persistinfo1.asp in a directory on your Web server (for example, C:\InetPub\Wwwroot\Persistinfo1.asp).
  3. Create a final ASP page named Persistinfo2.asp, and paste the following code:
    <html> 
    <head> 
    <title>Receive User Information 2</title> 
    </head> 
    <body> 
    <p>Received information from the QueryString. </p> 
    <p>Your name is: <%=Request.QueryString("Name")%></p> 
    <p>Your age is: <%=Request.QueryString("Age")%></p> 
    </body> 
    </html>
    					
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.
  2. 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

Modification Type:MinorLast Reviewed:7/2/2004
Keywords:kbhowto kbHOWTOmaster KB300505 kbAudDeveloper