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".

有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top