3

I'm creating an application which uses request.querystring to transfer variables between pages. However at the moment I am setting variable names in the URL dynamically in accordance with an array and I want to retrieve these values on another page using (essentially) the same array. So I came up with what I thought would be the simple solution:

For Each x In AnswerIDs
    response.write(x)
    ctest = Request.Querystring(x)
    response.write(ctest)
Next

However this doesn't seem to work as it looks like you can't use Request.Querystring() with a variable as it's parameter. Has anyone got any idea how I could do this?

This is the query string:

QuizMark.asp?11=1&12=1

In this case AnswerIDs consists of "11" & "12" so in my loop I want it to write "1" & "1".

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
howdybaby
  • 287
  • 1
  • 4
  • 15
  • Can you add the querystring you're using? That might give some more insight in your problem. – kloarubeek Feb 23 '14 at 14:28
  • What you are trying to do should work but if it isn't, just a guess, but as Request.QueryString is expecting a string for the key try: ctest = Request.Querystring(CStr(x)) – johna Feb 24 '14 at 02:15
  • @John no I just tested, and numbers can be keys. The problem is probably in the confusing display that using the given example will be plain "111121". – Shadow The GPT Wizard Feb 24 '14 at 08:01

2 Answers2

8

You can loop through the querystring like this:

For Each Item in Request.QueryString
    Response.Write Item & ": " & Request.QueryString(Item) & "<br/>"
Next

Item contains the name of the querystring parameter, with Request.Querystring(Item) you retrieve the value of the parameter.

kloarubeek
  • 2,706
  • 20
  • 24
0

Your method will work with a simple modification:

For Each x In AnswerIDs
    response.write(x)
    ctest = Request.Querystring(CStr(x))
    response.write(ctest)
Next

If your array might have Null elements, then replace the Cstr(x) with x & "".

If, on the other hand, you can make sure that every element of AnswerIDs has a value (i.e. is neither Empty nor Null), then you can omit the string conversion. In other words, the problem isn't with calling Request.Querystring with a variable, but with calling Request.Querystring with an Empty or a Null.

Martha
  • 3,932
  • 3
  • 33
  • 42