views:

202

answers:

1

I need to send a JavaScript array via querystring to my .ashx. In my .ashx (handler) I'll have a method that will need to parse through each value in the array and populate a generic List

Just trying to think about the way to go about this both passing it to the querystring as well as how to take that querystring value and convert it to a C# array in order to pass that into my method in my .ashx.

So first to pass the array to querystring, I assume it's going to be something like this

var javascriptArray = [1212, 32321, 42342];

now pass it to querystring first iterate through each value in the array and append to a variable in javascript. So I'd end up with something like this

var querystringArray = "1212, 32321, 42342";

and eventually querystringArray will be passed as a querystring parameter to the url that hits my .ashx.

Second, once I grab that parameter, assume I'll just use or create a ultility function that will do a string split and shove it into and return a C# array. Then pass that to my method.

Just wondering if there is an easier or better way to do all this. Thoughts? I'm not the greatest with JavaScript syntax yet.

+1  A: 

Javascript

var myArray = [12, 34, 56];
var url = "MyHandler.ashx?dat=" + myArray.join();

C#

// In Page_Load
string[] dat = Request.QueryString["dat"].Split(",");
Josh Stodola
Nice. I already wrote the split variable on the C# side but did not know you could do .join(). cool.
CoffeeAddict
Yep. By default, the join uses a comma, but you can pass it anything to use, like this: myArray.join(" ") for a space.
Josh Stodola