Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using ASP.net to develop this website and i need to pass my arraylist to javascript so that i can use those numbers and values to create a chart using jqPlot.

i Have a class called,
public class ConfidenceLvl
{

    private string ip;
    private string isp;
    private int conLvl;

    public string Ip
    {
        get { return ip; }
        set { ip = value; }
    }
    public string Isp
    {
        get { return isp; }
        set { isp = value; }
    }
    public int ConLvl
    {
        get { return conLvl; }
        set { conLvl = value; }
    }

    public ConfidenceLvl(string ipVal, string ispVal, int conLvlVal) {
        this.conLvl = conLvlVal;
        this.ip = ipVal;
        this.isp = ispVal;
    }
}


and i use this code to insert the values to my arraylist,

ArrayList conRc = new ArrayList();


while (readIp.Read())
{
    string ipVal = readIp.GetString(0);
    string ispVal = readIp.GetString(1);
    int conLvlVal = readIp.GetInt32(2);

    conRc.Add(new ConfidenceLvl(ipVal,ispVal,conLvlVal));

}
correlate.Value = ArrayListToString(ref conRc);

And this is the function that is used to format the array to pass to the javascript,

private string ArrayListToString(ref ArrayList _ArrayList)
   {
       int intCount = 0;
       string strFinal = "";

       foreach (object obj in _ArrayList)
       {
           ConfidenceLvl cnLv = (ConfidenceLvl)obj;
           String ip = cnLv.Ip;
           String isp = cnLv.Isp;
           int conLvl = cnLv.ConLvl;
           if (intCount > 0)
           {
               strFinal += "~";
           }
           strFinal += "IP:" + ip + " ISP:" + isp + " Confidence:" + conLvl.ToString();

           intCount += 1;
       }

       return strFinal;
   }


Ans this is the html side code that captues the value passed,

<asp:HiddenField ID="correlate" value= "" runat="server" />
<script language="javascript" type="text/javascript">
            var iplistString = document.getElementById('correlate').value;
            var iplistArray = iplistString.split('~');
            for (var i = 0; i < iplistArray.length; i++) {
                alert(iplistArray[i]); 
            }
        </script>

But this bets it as a whole string not seperate values,

I want to pass this to javascript so the can use that integer value (conLvl) to plot the graph using javascript jqPlot. I tried the HiddenField method but it gets all these values as a string. Please help on this issue..Thank you very much :)
Posted
Comments
dhl_hh 29-Aug-12 7:10am    
CORRECTION : *And this is the html side code that captures the value passed,

1 solution

You are almost there. Refer the link below:
Passing arrayList to javascript function[^]

And view the similar thread[^].



--Amit
 
Share this answer
 
Comments
dhl_hh 29-Aug-12 9:18am    
Thank you for your reply, i will check on your answer and bet back :) thank you very much :)
dhl_hh 29-Aug-12 11:01am    
This is somewhat i need. may i know how to manipulate this json string [{"Ip":"0","Count":"10"},{"Ip":"1","Count":"11"},{"Ip":"2","Count":"12"},{"Ip":"3","Count":"13"},{"Ip":"4","Count":"14"},{"Ip":"5","Count":"15"},{"Ip":"6","Count":"16"},{"Ip":"7","Count":"17"},{"Ip":"8","Count":"18"},{"Ip":"9","Count":"19"}] in javascript..lets say to get the third ip address?? this is what i got after referring to your help :) thank you very much
_Amy 29-Aug-12 11:27am    
Sorry friend, I have no idea about this. All the best.Try with google. May be you'll get it.. :)
dhl_hh 29-Aug-12 12:31pm    
yeaaa...i just found how to do it :) thank you very much for your help :)
_Amy 29-Aug-12 23:47pm    
Welcome. :)

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