Get ASP.Net (C#) ArrayList into JavaScript Array (Using Ajax)

Now I am going to discuss how can I get aASP.Net (C#) ArrayList into JavaScript Array.

Step 1 :

Lets create a ASP.Net (C#) ArrayList in .aspx.cs (Lets the name of  ‘ .aspx.cs ‘ for this example is Default.aspx.cs).

Then add some items in that ArrayList :

public static Array GetAllItems()
ArrayList arrayList = new ArrayList();
return arrayList.ToArray();

**For  [WebMethod] we have to add ” using System.Web.Services “.**

Step 2 :

Now I am going to write an ajax function in my Default.aspx page.

function getItems() {
type: “Post”,
url: “Default.aspx/GetAllItems”,
contentType: “application/json; charset=utf-8”,
dataType: “json”,
success: function (response) {
var items = response.d;
for (var i = 0; i < items .length; i++) {
alert(items [i]);

failure: function (msg) {

Next I am going to call this getItems() function via a HTML button,

<input type=”Button” value=”Click” onclick=”getItems()”/>

Now if we click on this button then we can see ‘Item1’, ‘Item2’ ‘Item3’ in alert message.

Leave a Reply