0

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.

1
  • See JSON Commented Feb 12, 2012 at 9:07

1 Answer 1

3

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]);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

what's the reference class new to add to call "JavaScriptSerializer" function ?
@PankajGarg, here it is: msdn.microsoft.com/en-us/library/… . Namespace: System.Web.Script.Serialization, Assembly: System.Web.Extensions.dll.
+1 for JavaScript Serializer. If you try to pass the argument without serialize it will give the illegal character runtime error at Javascript console

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.