How to loop through a generic list using javascript

Last post 07-03-2007 5:45 PM by KaziManzurRashid. 4 replies.

Sort Posts:

  • How to loop through a generic list using javascript

    11-01-2006, 10:17 AM
    • Member
      40 point Member
    • codewzrd
    • Member since 11-19-2004, 1:55 PM
    • Posts 8

    Hi

    I've installed the AJAX Beta 1. I have a web service that returns a List<CustomClass>. The list is currently a dummy list made up of 2 items.

    The custom class is:

    CustomClass
    {
        public string str1;
        public string str2;
    }
    
    When I put a break point on the onComplete javascript callback function, I see both items in the debugger but I don't know how to get the count of items in the list. The list.Count is undefined. Count is available in C# so I thought it would map over to javascript.

    Any ideas? 
  • Re: How to loop through a generic list using javascript

    11-02-2006, 4:23 AM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • TrustedFriends-MVPs

    Hi,

    I think a List is serialized into a JavaScript array, therefore you should be able to do

    var len = myList.length;

    to get its size. 

    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: How to loop through a generic list using javascript

    11-02-2006, 9:38 AM
    • Member
      40 point Member
    • codewzrd
    • Member since 11-19-2004, 1:55 PM
    • Posts 8

    Thanks for the help. I figured it out a few minutes after posting my question. I had to go to W3C's website to look up the JavaScript syntax/tutorials on Arrays.

    I just wish that the intellisense for JavaScript would have shown me that property.

     

  • Re: How to loop through a generic list using javascript

    07-03-2007, 3:28 PM
    • Member
      91 point Member
    • brendalisa
    • Member since 11-29-2006, 4:02 PM
    • Posts 40

    Will you post your solution on how you did this?  I'm passing a List back to Javascript byt using a Callback, but I need to loop through the list. 

     Thanks!

    Brenda
  • Re: How to loop through a generic list using javascript

    07-03-2007, 5:45 PM
    • Contributor
      4,792 point Contributor
    • KaziManzurRashid
    • Member since 03-09-2003, 11:04 AM
    • Dhaka, Bangladesh
    • Posts 882

    try this:

    for(var i = 0; i < YourList.length; i++)
    {
    var yourObject = YourList[i];

    //Do whatever you want.
    }

    Long Live .NET
    Kazi Manzur Rashid (Amit)
    _________________________
    Web: http //dotnetshoutout.com
    Blog: http://weblogs.asp.net/rashid
    Twitter: http://twitter.com/manzurrashid
Page 1 of 1 (5 items)