To read the article online, visit http://www.4GuysFromRolla.com/webtech/101999-1.shtml

Passing Arrays from One ASP Page to Another


Session variables are evil. Quite evil. We should strive to create our ASP pages without the use of session variables. However, session variables make it easy to pass information from one ASP page to another. It is recommended, however, that you use the querystring or forms to pass information from one ASP page to another, when possible. (There is an article on Four Ways of Passing Information From One ASP Page to Another.)

When passing strings or numbers, using a form or the querystring is a viable option. Many developers, though, think that they need to use session variables to pass arrays from one ASP page to another. This is far from the truth! To pass arrays, though, we have to use a bit of clever coding.

Before we begin, let's set up the situation. We have two ASP pages, CreateArray.asp and ReceiveArray.asp. In CreateArray.asp we will create an array of some size, enter some values, and allow this information to be passed to ReceiveArray.asp through both the querystring and through the use of a form. In ReceiveArray.asp, we will read in the array being passed in and display it.

Let's start by creating the array in CreateArray.asp

   'Create an array that contains
   'the days of the week
   Dim aWeekDays(6)
   aWeekDays(0) = "Sunday"
   aWeekDays(1) = "Monday"
   aWeekDays(2) = "Tuesday"
   aWeekDays(3) = "Wednesday"
   aWeekDays(4) = "Thursday"
   aWeekDays(5) = "Friday"
   aWeekDays(6) = "Saturday"

Now, to store the array contents in the querystring, we will loop from LBound(aWeekDays) to UBound(aWeekDays). For each element, we'll add an item to the querystring, having the array name, an equals sign, and then the value of the particular array element.

   'Build the querystring
   Dim iLoop, strQueryString
   For iLoop = LBound(aWeekDays) to UBound(aWeekDays)
      strQueryString = strQueryString & "WeekDays=" & aWeekDays(iLoop)
      strQueryString = strQueryString & "&"
   Next

   'Cut off the last character
   strQueryString = Left(strQueryString, Len(strQueryString) - 1)

   'Now, create the actual hyperlink
%>

   <A HREF="ReceiveArray.asp?<%=strQueryString%>">
        ReceiveArray.asp
   </A>

We can also pass the contents of an array through a form, using the HIDDEN INPUT tag. When using a form, we want to create N HIDDEN INPUT tags, given N elements in our array. Each INPUT tag will have the same NAME but different VALUEs.

   <FORM METHOD=POST ACTION="ReceiveArray.asp">
<%
   For iLoop = LBound(aWeekDays) to UBound(aWeekDays) %>
      <INPUT TYPE=HIDDEN NAME=WeekDays VALUE="<%=aWeekDays(iLoop)%>">
<%   Next %>
     <P>
     <INPUT TYPE=SUBMIT>
   </FORM>

When the above form is submitted, the array information will be passed to ReceiveArray.asp via the POST METHOD. A screenshot of CreateArray.asp can be seen at the end of this article.

Now that we've looked at how to encode the array to pass it to ReceiveArray.asp, let's examine how we will decode the array. Part 2 will delve into that topic!

  • Read Part 2

    CreateArray.asp
    Screenshot of CreateArray.asp

  • Article Information
    Article Title: Passing Arrays from One ASP Page to Another
    Article Author: Scott Mitchell
    Published Date: Tuesday, October 19, 1999
    Article URL: http://www.4GuysFromRolla.com/webtech/101999-1.shtml


    Copyright 2014 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers