1

In ASP.NET, I have collected a list of selected items from a checkbox in an array at the client side. Now I need to pass the array from client to server's ASMX web method. How do I do this?

6
  • Is it an ASMX Web Service you're using on the backend? Are you using any JavaScript libraries? Commented Sep 29, 2011 at 7:32
  • yes,using ASMX web service & jquery Commented Sep 29, 2011 at 7:34
  • what is the question? is it a trouble of converting a List to an Array (solution: List<T>.ToArray() ) or what? Commented Sep 29, 2011 at 7:34
  • 1
    The problem is, i have the contents in an array at client side,I need to pass that array to web method and convert that array into list on the server. But i have trouble in passing array from client to server side's web method. Commented Sep 29, 2011 at 7:38
  • 1
    possible duplicate of how do you pass an array string[] to a Web Service via Jquery? Commented Sep 29, 2011 at 8:03

2 Answers 2

1

Add a JSON library to your page, use json2.js here. That gives you a function to serialize javascript arrays into JSON strings.

You can then pass it into your webmethod:

[WebMethod] 
public void MyWebMethod(List<string> someValues)
{
    // Use someValues...
}

Here's the javascript you need

var arrayData = ["1","2","3"]; // Your array goes here

$.ajax({
  type: "POST",
  url: "MyWebService.asmx/MyWebMethod",
  data: JSON.stringify({ someValues: arrayData }),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function() 
  {
    // Your success function...
  }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Client-Side Web Service Calls with AJAX Extensions

Calling Web Services from Client Script

Comments

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.