Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 20, 2012 09:36 AM by PGChoudhury
Dec 20, 2012 06:25 AM|LINK
I have a situation which I will describe here. Suppose I have a url which looks like the following:
Now how do I get these parameter values and map them with appropriate variables. I mean in case of a single parameter, we'd do a Request.QueryString["search"].ToString().
I mean there are multiple parameters and we'd want to map them to the correct variables so that we can implement some business logic with the parameters. Sorry if my query appears to be of basic nature, but I am slightly confused. I suppose we need to implement
some looping mechanism based on the position/index of the '&' character. I could be wrong though.
Need some active assistance on this issue from our fabulous forum members.
Many Thanks in anticipation.
Dec 20, 2012 06:33 AM|LINK
The Request.QueryString property is a name value collection, so you can use a for/foreach loop to iterate through that.
You can use some thing like
int loop1, loop2;
// Load NameValueCollection object.
// Get names of all keys into a string array.
String arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
Response.Write("Key: " + Server.HtmlEncode(arr1[loop1]) + "<br>");
String arr2 = coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
foreach (string key in Request.QueryString.Keys)
Response.Write(key+ ” “+Request.QueryString[key]+”<br/>”);
Dec 20, 2012 07:05 AM|LINK
Many thanks for the code snippet.
The second approach uses forech from the IEnumerable interface to iterate. Which one according to you should I with perspective to faster execution time?
Dec 20, 2012 07:53 AM|LINK
From my opinion both are same. In the first example, i have declared a namevalue collection and assigned the query string and in the second i just iterate through the keys of querystring. You can go with the second approach as it is clean compare to the
Dec 20, 2012 09:36 AM|LINK
Thanks for the clarification.
I will go for the 2nd approach. It's cleaner.