ASP.NET 2.0 QueryString - One Key with multiple values

There are 2 ways that I know that you can set multiple values for 1 key. Example,

You have a unique key named pid which stands of program id. Basically within your page, you have selected a few program id maybe from a GridView and you decide to pass this over to the next page using QueryString. Let’s not argue why use QueryString for this. I just want to show you how you can achieve this. Nothing else.

I spoke to a few developers. Basically what they recommend are this:

Response.Redirect(“Default2.aspx?pid=1.01,2.01,3.01”, false);

So in your c# code, basically this how you code to retrieve the program id.

string[] strSample = Request.QueryString["pid"].Split(',');

It makes sense here why you use the Split method. Basically each program id is delimited by the comma. So by using split here, you will get what you want.

Results:

strSample[0] = 1.01

strSample[1] = 2.01

strSample[2] = 3.01

Trying this code won’t work as you want.

Response.Write(Request.QueryString["pid"][0].ToString() + “ – “);

Response.Write(Request.QueryString["pid"][1].ToString());

By running that code, you will get the results as below:

1   .

However, if you read the MSDN Documentation here http://msdn2.microsoft.com/en-us/library/aa479019.aspx, it recommends us to use this instead.

// Extracted from MSDN //

For example, in ASP the individual query string values from a request to http://localhost/myweb/valuetest.asp?values=10&values=20 would be accessed as follows:

Copy Code

<% 'This will output "10" Response.Write Request.QueryString("values")(1) 'This will output "20" Response.Write Request.QueryString("values")(2) %>

In ASP.NET, the QueryString property is a NameValueCollection object from which you would need to retrieve the Values collection before retrieving the actual item you want. Again, note the first item in the collection is retrieved by using an index of zero rather than one:

Copy Code

<% 'This will output "10" Response.Write (Request.QueryString.GetValues("values")(0)) 'This will output "20" Response.Write (Request.QueryString.GetValues("values")(1)) %>

////////////////////////////

Instead of using this type of QueryString:

Response.Redirect(“Default2.aspx?pid=1.01,2.01,3.01”, false);

Try this instead

Response.Redirect(“Default2.aspx?pid=1.01&pid=2.01&pid=3.01”, false);

If you look at the MSDN code as above, you will never able to get it work.

Try this instead:

Response.Write(Request.QueryString.GetValues("pid").GetValue(0) + " - ");

Response.Write(Request.QueryString.GetValues("pid").GetValue(1) + " - ");

Response.Write(Request.QueryString.GetValues("pid").GetValue(2));

First you use GetValues(“pid”) which you need to specify the key “pid”. Then you use GetValue(0) as you get the 1st value for that key.

Conclusion, there is no right or wrong here. It is up to you to decide what is best for you. Oh yes, it is possible to have more than one values for 1 key. Hope you find it useful.

Published Thursday, March 08, 2007 9:39 AM by chuawenching
Filed under: ,