0

Is it possible to pass multiple values to a single Querystring parameter? Here is what I am thinking:

index.aspx?loc=wi&mi&mn

or

index.aspx=?loc=wi&loc=mi&loc=mn

Something like that. I am sure I could pass them in other ways and possibly explode the inner contents (sorry - split would be better .net term). But I would rather do this in a way that I could easily extract via code similar to this:

for i = 1 to request.querystring("loc").count
    'do some stuff here
next i

2 Answers 2

0

You can pass multiple values by using a separator between them.

C#

var locations = new List<string> { "wi", "mi", "mn" };
var locationsCommaSeparated = string.Join(",", files);
Response.Redirect("index.aspx?loc=" + locationsCommaSeparated);

VB

Dim locations = New List(Of String)() From { _
    "wi", _
    "mi", _
    "mn" _
}
Dim locationsCommaSeparated = String.Join(",", files)
Response.Redirect("index.aspx?loc=" + locationsCommaSeparated)

On the receiving end, you can then split them back out.

C#

var locationsCommaSeparated = Request.QueryString["loc"].ToString();
var locations = locationsCommaSeparated.Split(',');

foreach(var location in locations)
{
    //do something
}

VB

Dim locationsCommaSeparated = Request.QueryString("loc").ToString()
Dim locations = locationsCommaSeparated.Split(","C)

For Each location As var In locations
    'do something
Next

Note, I'm a C# programmer, so I used an automatic code converter to convert it to VB. Results may not be idiomatic valid VB.

Careful care would need to be taken if the value you're passing would contain the character you're using as a delimiter. Since it appears you're just passing state abbreviations, you should be safe in this particular instance.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again. I basically just put commas in and exploded out the array of strings, then ran a for-next loop on the array. Worked out perfectly. My querystring then become ?loc=wi,mi,mn
0

try this

    ArrayList arr = new ArrayList();
    arr.Add(“file1″);
    arr.Add(“file2″);
    arr.Add(“file3″);
    string arry = String.Join(“,”, ((string[])arr.ToArray(typeof(String))));
    Response.Redirect(“index.aspx?file=” + arry);

8 Comments

I'm trying to use and read multiple parameters. I'm not sure I understand how your code helps that? Maybe I am missing something, sorry :(
@Andrew Did you try this solution? It's combining multiple values into a comma separated list and so passing multiple parameters in a key/value pair. Exactly what you're looking for.
@Mohammad Why are you using ArrayList and a lot of unnecessary casting? Modern C# uses generic lists. Also, be careful not to use the decorative double quotes “ ″ in your code, as they aren't valid C#. Use " instead.
I see what you are doing, but I am reading from querystring only. I don't need to generate the querystring.
@mason You are right.In above example i am using ArrayList because of simplicity. So he can understand easily.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.