Click here to Skip to main content
13,086,662 members (56,248 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have 3 arrays at my code behind:
ArrayList ID = new Arraylist();
ArrayList name = new Arraylist();
ArrayList age = new Arraylist();

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
name: C
age: 13

how can this be done in a javascript for loop.
Posted 9-Oct-12 21:06pm
Updated 9-Oct-12 21:22pm
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
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

Code behind: Default.aspx.cs
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.

<%@ 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)); %>)');
<asp:Content ID="MainContent" runat="server" ContentPlaceHolderID="MainContent">
 <script type="text/javascript">

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.
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 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100