views:

3893

answers:

5

I am trying to loop through a query string and pull out certain values as in:

?ProductID=1234&ProductID=4321&Quantity=1

For each value next to ProductID I want to execute some logic. But I am not sure how to get to the values. Any ideas?

A: 
sQS = Request.QueryString
For Each eItem in Split( sQS, "&" )
sName = Left( eItem, Instr( eItem & "=", "=" ) - 1 )
Response.Write sName _
& " = " & Request.QueryString( sName ) _
& "<br>"
Next

grabbed that here.. http://bytes.com/groups/asp/158709-loop-round-all-querystring-parameters

madcolor
A: 

Try this one. Only works in VB9.

Dim queryString = GetQueryString()
queryString = queryString.SubString(1) 'Remove ?
Dim ids = queryString. _
  Split("&"c). _
  Select(Function(x) x.Split("="c)). _
  Where(Function(x) x(0) = "ProductId" ). _
  Select(Function(x) x(1))
JaredPar
+2  A: 

Here is some untested psuedo code that should work for the code behind on the page. I hope this helps.

dim key as string
dim list as new arraylist()
for each key in Page.Request.QueryString.Keys
 if key = "ProductID" then
   list.add(Page.Request.QueryString(key))
 end if
next key

' do somthing with the list of product id's
James
+2  A: 
Dim productID = Request.Querystring("ProductID");
Dim quantity = Request.Querystring("Quantity");
Chris Ballance
VB doesn't support square brackets, that's C#.
James
Thanks for the reminder, I don't speak much VB.
Chris Ballance
+3  A: 

When your query string has more than one value with the same key you can use the NameValueCollection.GetValues method which returns a string array:

dim productID as string
for each productID  in Page.Request.QueryString.GetValues("ProductID")
  ' do something with productID
next productID
Jason DeFontes
Worked like a charm thx.
Jim