Click here to Skip to main content
15,915,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 arrays at my code behind:
C#
ArrayList ID = new Arraylist();
ID.Add("1");
ID.Add("2");
ID.Add("3");

ArrayList name = new Arraylist();
name.Add("A");
name.Add("B");
name.Add("C");

ArrayList age = new Arraylist();
age.Add("16");
age.Add("29");
age.Add("13");


I already pass the array to my javascript, but the problem is how can it display in this format:

ID: 1
name: A
age: 16

ID: 2
name: B
age: 29

ID:3
name: C
age: 13


how can this be done in a javascript for loop.
Posted
Updated 9-Oct-12 21:22pm
v2
Comments
Zoltán Zörgő 10-Oct-12 3:12am    
And what is your JS code currently? How do you pass the arrays to the JS side?
melvintcs 10-Oct-12 3:23am    
function test(){
alert("<%=ID.Count %>");
}
Zoltán Zörgő 10-Oct-12 3:28am    
Just to be clear: this way you don't pass the arrays to js, you render your html with the values replaced. This approach is wrong. You either render the whole arrays as javascript objects (json encoded for example) and process them with javascript, or you render the elements directly in the html from asp.net.
melvintcs 10-Oct-12 3:40am    
so i need to use json to pass the whole array? i never use json before, any suitable tutorial for my case?

1 solution

The following is a really minimalistic approach, assuming an asp.net project from the VS template.

Code behind: Default.aspx.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        public sealed class Person 
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }

        protected IList<person> persons = new List<person>();

        protected void Page_Load(object sender, EventArgs e)
        {
            persons.Add(new Person { ID = 1, Name = "Peter", Age = 20 });
            persons.Add(new Person { ID = 2, Name = "John", Age = 26 });
            persons.Add(new Person { ID = 2, Name = "Olivia", Age = 23 });
        }
    }
}

As you can see I am defining a class, and a list from that class, not an array. And of course, not three arrays, since that is a really bad approach. So I have my data.
Now, in the Defaul.aspx I embed the JSON encoded representation of that object. I actually pass it to a javascript object.

XML
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="ScriptContent" runat="server" ContentPlaceHolderID="HeadContent">
 <script type="text/javascript">
  var persons = eval('(<% Response.Write(fastJSON.JSON.Instance.ToJSON(persons)); %>)');
 </script>
</asp:Content>

<asp:Content ID="MainContent" runat="server" ContentPlaceHolderID="MainContent">
 <script type="text/javascript">
     alert(persons);
 </script>
</asp:Content>

The alert will show an array of objects. Now it is your turn to process the variable. I have downloaded and referenced FastJSON[^] for this code to work. Notice, that you can add a second parameter to the serializer to fine tune the serialization. Look at the generated code to see how the serialization is performed.

Well, this is not the only approach, and could be not the most suitable for your needs. But you have not specified what you want to do.
 
Share this answer
 
v4
Comments
melvintcs 10-Oct-12 21:33pm    
i downloaded FastJSON, but inside has so many folders. which one to include as reference?
Zoltán Zörgő 11-Oct-12 0:45am    
Build it and reference fastjson.dll.
melvintcs 11-Oct-12 1:04am    
i did, but my test.cs got a lot of error while building. however it got fastjson.dll(v 1.0.0) inside the bin folder, i reference that one. but there is still a error said
"The type or namespace 'Instance' does not exist in the namespace 'fastJSON' (are you missing an assembly reference?)"
Zoltán Zörgő 11-Oct-12 3:16am    
Sorry, but I can't see your screen, thus I can't imagine what you are doing wrong. But since you have a "test.cs" in an asp.net project, there could be a problem. As I suggested, start with the VisualStudio template first. If it is running on it's own, try the code above. And if that's working, only than go on with your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900