Warrior
Warrior

Reputation: 139

how to access arraylist, string array, or list from javascript?

I want to call one javascript function from my button click on server side and pass the arraylist, string array or list as a parameter to the function. I would then need to iterate through all the elements of these collection and perform some action on the data. Can you tell me the javascript code to access these element Consider the following structure that I have in the Javascript

Function ABC(_Arraylist/List/String Array){

//I would like to perform some action on the collections data here.
}

Thanks.

Upvotes: 0

Views: 3347

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039200

Server:

// this could also be a string[] if you prefer
var array = new List<string> { "foo", "bar", "baz" };
var serializer = new JavaScriptSerializer();
var script = string.Format("foo({0});", serializer.Serialize(array));
ClientScript.RegisterStartupScript(GetType(), "call", script, true);

Client:

function foo(array) {
    for (var i = 0; i < array.length; i++) {
        alert(array[i]);
    }
}

Upvotes: 3

Related Questions