Bristle Software ASP Tips

This page is offered as a service of Bristle Software, Inc.  New tips are sent to an associated mailing list when they are posted here.  Please send comments, corrections, any tips you'd like to contribute, or requests to be added to the mailing list, to tips@bristle.com.

Table of Contents:

  1. Passing Info Between Pages
    1. Request.QueryString
    2. Request.Form
    3. Request "Collection"
    4. Session Variables
    5. Application Variables
  2. Invoking One Page From Another
    1. Response.Redirect
  3. Debugging
    1. Using Visual InterDev as an ASP Debugger
  4. ASP+
    1. Intro to ASP+
    2. ASP vs ASP+

Details of Tips:

  1. Passing Info Between Pages

    1. Request.QueryString

      Last Updated: 7/18/1999
      Applies to: IIS 4.0+

      The simplest way to pass parameters to an ASP page is by appending them to the URL itself as:

      	http://www.site.com/file.asp?param1=53&p2=97&aparam=a+string

      This passes the string "53" as the value of param1, the string "97" as the value of p2, and the string "a string" as the value of aparam.   Note that the parameter names are defined by the URL, all values are passed as strings, question mark ("?") precedes the first parameter, ampersand ("&") separates parameters, equals ("=") separates parameter name from value, and plus ("+") is a placeholder for a blank space in a value.

      In the ASP page, such parameters are retrieved via the QueryString collection of the Request object.  In the example above, the following VBScript code retrieves the parameters:

      	strParam1 = Request.QueryString("param1")
      	strParam2 = Request.QueryString("p2")
      	strParam3 = Request.QueryString("aparam")

      You can also pass multiple values for the same parameter, as:

      	http://www.site.com/file.asp?p1=10&p1=20&p1=25

      In such a case, the VBScript:

      	strParam1 = Request.QueryString("p1")

      retrieves all values together in the single string "10, 20, 25".  You can retrieve the values separately via the VBScript:

      	strParam1_1 = Request.QueryString("p1")(1)
      	strParam1_2 = Request.QueryString("p1")(2)
      	strParam1_3 = Request.QueryString("p1")(3)

      This method of passing parameters is used automatically by any HTML page with a tag like:

      	<FORM action="file.asp" method="get">	

      When the user clicks on an HTML button to submit the form, the values of all HTML input fields, option buttons, lists, etc. on the form are appended to the URL when the next page is requested from the server.  The action attribute specifies the URL of the next page to be requested.  The value "get" for the method attribute specifies that the parameters are to be passed as part of the URL.

      Beware using this approach for large number of parameters or parameters with long values.  Some Web browsers don't handle long URLs well.  For example, IE 4.0 experiences serious problems (access violations, infinite loops, etc.) if an URL exceeds 2083 bytes.  Netscape 3.0 has no such problem.

      Also, keep in mind that parameters passed this way are visible to the user as part of the URL.

      --Fred

    2. Request.Form

      Last Updated: 9/12/1999
      Applies to: IIS 4.0+

      Another way to pass parameters to an ASP page is to use the "post" method of the FORM tag, as:

      	<FORM action="file.asp" method="post">	

      When the user clicks on an HTML button to submit the form, the values of all HTML input fields, option buttons, lists, etc. on the form are sent to the target page as Unix-style "standard input".  The action attribute specifies the URL of the next page to be requested.  The value "post" for the method attribute specifies that the parameters are to be passed via standard input.

      In the ASP page, such parameters are retrieved via the Form collection of the Request object.  For example, the following VBScript code retrieves the values of HTML controls param1, p2, and aparam:

      	strParam1 = Request.Form("param1")
      	strParam2 = Request.Form("p2")
      	strParam3 = Request.Form("aparam")

      As with "get" and QueryString, each HTML control can pass multiple values (especially the HTML SELECT tag with the multiple attribute).  In such a case, the VBScript:

      	strParam1 = Request.Form("p1")

      retrieves multiple comma-separated values together in a single string, and the VBScript:

      	strParam1_1 = Request.Form("p1")(1)
      	strParam1_2 = Request.Form("p1")(2)
      	strParam1_3 = Request.Form("p1")(3)

      retrieves the first 3 individual values.

      This technique automatically passes the values of all "controls" on an HTML form, but how do you get it to pass additional values that are not displayed to the user in the current form?  Use "hidden" controls.  for example:

      	<input type=hidden name="p1" value="value of p1">

      Thanks to Steve Weitzman and Bob Durbin for suggesting hidden fields!

      --Fred

    3. Request "Collection"

      Last Updated: 12/3/1999
      Applies to: IIS 4.0+

      The target ASP page doesn't have to know whether parameters were passed to it via the "post" or "get" method.  Rather than retrieving the parameters explicitly from the QueryString or Form collection of the Request object, it can simply treat the Request object itself as a collection.  Replace either of the following:

      	strParam1 = Request.QueryString("param1")
      	strParam1 = Request.Form("param1")

      with simply:

      	strParam1 = Request("param1")

      This retrieves from the QueryString collection unless that collection has no value with key "param1", in which case, it retrieves from the Form collection.

      As with QueryString and Form, each key can have multiple values.  In such a case, the VBScript:

      	strParam1 = Request("p1")

      retrieves multiple comma-separated values together in a single string, and the VBScript:

      	strParam1_1 = Request("p1")(1)
      	strParam1_2 = Request("p1")(2)
      	strParam1_3 = Request("p1")(3)

      retrieves the first 3 individual values.

      Note that the Request object is not really a collection, and does not really have a default parameterless property that returns a collection.  It has a default property that takes a string parameter and returns a collection.  This has the following ramifications:

      1. You cannot use this technique to get values for the same key from both QueryString and Form.  If there are any values for the specified key in the QueryString collection, any values for that key in the Form collection are hidden.   You can still fetch them explicitly via Request.Form, but they don't show up in the Request collection.
      2. You can use this technique to fetch values for one key from QueryString and values for another key (that has no QueryString values) from Form.
      3. You cannot refer to Request.Count, as you can Request.QueryString.Count and Request.Form.Count.
      4. You can refer to Request("p1").Count to determine how many values there are for the key "p1".
      5. You cannot use Request in a For Each loop, as you can  Request.QueryString and Request.Form.
      6. You can use Request("p1") in a For Each loop.

      Thanks to Steve Weitzman for this tip!

      Caveats:  Dave Armstrong pointed out that this applies not only to the QueryString and Form collections, but also to the Cookies, ClientCertificate, and ServerVariables collections.  That is, each reference to Request("p1") will search all five collections, in the order shown above, until it finds a match.   In my opinion, this is a disadvantage for the following 2 reasons:

      1. It clutters the namespace.  You have to be careful to avoid accidentally picking up the value of a ClientCertificate, for example, when no value is specified as the value of a control on a Form or as a URL parameter.
      2. It is relatively slow to search all 5 collections to find out that no value was specified.

      See the documentation for more details.  This is described on the October 1999 MSDN Library CD under:
          Platform SDK
              Web Services
                  Internet Information Services SDK
                      Active Server Pages Guide
                          Visual Basic Object Model
                              Request Object

      --Fred

    4. Session Variables

      Last Updated: 7/18/1999
      Applies to: IIS 4.0+

      Session variables can be used to pass data between ASP pages in the same user's session.  The first page defines and values the session variable by adding to the Session collection as:

      	Session("varname") = "value1"

      The second page retrieves the value as:

      	strVar1 = Session("varname")

      Session variables share data between pages accessed by a single user during a single "session" (as defined by the Web server).  Data is not shared across different users, but the pages can be from different "applications" (pages without a common root).

      --Fred

    5. Application Variables

      Last Updated: 7/18/1999
      Applies to: IIS 4.0+

      Application variables can be used to pass data between ASP pages in the same "application" (group of pages with a common root).  The first page defines and values the application variable by adding to the Application collection as:

      	Application("varname") = "value1"

      The second page retrieves the value as:

      	strVar1 = Application("varname")

      Application variables share data between pages from the same application.  Data is not shared across applications, but is shared between users of the same application.

      --Fred

  2. Invoking One Page From Another

    1. Response.Redirect

      Last Updated: 7/18/1999
      Applies to: IIS 4.0+

      An ASP page can conditionally redirect to another ASP page. 

      This is useful where an HTML page contains a single Form but multiple buttons that are intended to navigate to two different ASP pages.  Since the single Form can have only one target ASP page (the one specified in the action attribute of the FORM tag), both buttons must actually navigate to that page.  However, that page can be designed to conditionally redirect to another page, based on the incoming parameters, which can indicate which button was clicked.

      To determine which button was clicked, check which one passed its name/value pair as one of the parameters from the HTML page.  To redirect to another page, use the Redirect method of the Response object.  For example, define the 2 buttons on the HTML form as:

      	<input type="submit" value="OK" name="cmdOK">
      	<input type="submit" value="Details" name="cmdDetails">

      If the OK button is clicked, a parameter named cmdOK with value "OK" is passed to the target ASP page.  If the Details button is clicked, a parameter named cmdDetails with value "Details" is passed instead.   The VBScript code in the target ASP page looks like:

      	If Request.QueryString("cmdOK").Count <> 0 Then
      		GenerateTheOKPage
      	ElseIf Request.QueryString("cmdDetails").Count <> 0 Then
      		Response.Redirect "DetailsPage.asp" Then
      	End If		

      To the end user, the redirection is transparent.   It seems as if the two buttons on the HTML page navigate directly to two different ASP pages.

      --Fred

  3. Debugging

    1. Using Visual InterDev as an ASP Debugger

      Last Updated: 12/3/1999
      Applies to: IIS 4.0+

      Having trouble getting Visual InterDev 6 (VID6) to work as an ASP debugger?

      You can configure it to allow you to step through the ASP (server side) code from the client end.  You can even get it to pop up as a Just-In-Time (JIT) debugger.  In this mode, even if you don't have VID6 running, when you load an ASP page with an error (syntax error or runtime error) into the Browser (Netscape, IE, etc.), VID6 is invoked and displays the ASP source line where the error occurred, allowing you to step through the code, examine variables, etc.  It's great when it works, but a lot of options have to be set correctly.  The JIT mode is particularly fragile because some of the options are designed to reset themselves when you use the VID6 debugger in regular (non-JIT) mode.

      For info on how to configure it, see the 45-page Microsoft white paper:

          http://msdn.microsoft.com/library/techart/msdn_videbugging.htm

      The white paper is also available on the MSDN Library CDs as:
          Technical Articles
              Visual Tools
                  Visual InterDev
                      Microsoft Visual InterDev 6.0 Debugging            

      There is even a tool to help you figure out which of the myriad settings you got wrong:

          http://msdn.microsoft.com/library/techart/samples/5257.exe

      Thanks to Joe McPeak for pointing me to these resources!

      --Fred

  4. ASP+

    1. Intro to ASP+

      Last Updated: 8/18/2000
      Applies to: IIS 4.0+

      For a good summary of the features of ASP+, see:

          http://www.asp-zone.com/articles/ck072600/ck072600.asp

      --Fred

    2. ASP vs ASP+

      Last Updated: 8/18/2000
      Applies to: IIS 4.0+

      For a summary of the differences between ASP and ASP+, including a list of the things you'll have to change in your existing ASP pages to migrate to ASP+, see:

          http://www.asp-zone.com/articles/ck080300/ck080300.asp

      --Fred

©Copyright 1999-2014, Bristle Software, Inc.  All rights reserved.