Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have 3 arrays at my code behind:
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 9-Oct-12 22:06pm
Edited 9-Oct-12 22:22pm
enhzflep38.5K
v2
Comments
Zoltán Zörgő at 10-Oct-12 3:12am
   
And what is your JS code currently? How do you pass the arrays to the JS side?
melvintcs at 10-Oct-12 3:23am
   
function test(){
alert("<%=ID.Count %>");
}
Zoltán Zörgő at 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 at 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
good
Please Sign up or sign in to vote.

Solution 1

The following is a really minimalistic approach, assuming an asp.net 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)); %>)');
 </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.
  Permalink  
v4
Comments
melvintcs at 10-Oct-12 21:33pm
   
i downloaded FastJSON, but inside has so many folders. which one to include as reference?
Zoltán Zörgő at 11-Oct-12 0:45am
   
Build it and reference fastjson.dll.
melvintcs at 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ő at 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)

  Print Answers RSS
0 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2014
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