Q:

Accessing C# List variable in JavaScript

C#
public List<double> Coordinatesx = new List<Double>();
    public List<double> Coordinatesy = new List<Double>();

    protected void Page_Load(object sender, EventArgs e)
    {        
        DataTable dt = new DataTable();
         SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["TRY"].ConnectionString);
        string query = "select x,y from Addresses";
        cnn.Open();
        SqlCommand cmd = new SqlCommand(query, cnn);
        SqlDataReader id = cmd.ExecuteReader();
        while(id.Read())
        {
            Coordinatesx.Add(Convert.ToDouble(id[0]));
            Coordinatesy.Add(Convert.ToDouble(id[1]));
         }
         cnn.Close();

   }

JAVASCRIPT
function calculateinside()
    {

         for (var j = 0; j < <%=Coordinatesx.Count%>; j++) {
             var point = new google.maps.LatLng(<%=Coordinatesx%>[j],<%=Coordinatesy%>[j]);
             if(google.maps.geometry.poly.containsLocation(point,poly)){
                 marker2 = new google.maps.Marker({

                     position: point,
                     map: map,
                     icon: 'images/point2.png',

                 });
             }
      }

    }   }

I am trying to take variables from c# to javascript, I have tried that code but there is an error.
var point = new google.maps.LatLng(<%=Coordinatesx%>[j],<%=Coordinatesy%>[j]);

How can I access each value of Coordinatesx and Coordinatesy list?

A:

You must generate the arrays in the javascript code:
function calculateinside()
{
     var Coordinatesx = [<%=string.Join(",", Coordinatesx)%>];
     var Coordinatesy = [<%=string.Join(",", Coordinatesy)%>];
     for (var j = 0; j < Coordinatesx.lenght; j++) {
         var point = new google.maps.LatLng(Coordinatesx[j],Coordinatesy[j]);
         if(google.maps.geometry.poly.containsLocation(point,poly)){
             marker2 = new google.maps.Marker({

                 position: point,
                 map: map,
                 icon: 'images/point2.png',

             });
         }
     }
}   

if Coordinatesx = {1,2} and Coordinatesy = {5,7} in server-side code, it will generate the javascript like this:
function calculateinside()
{
     var Coordinatesx = [1,2];
     var Coordinatesy = [5,7];
     for (var j = 0; j < Coordinatesx.lenght; j++) {
         var point = new google.maps.LatLng(Coordinatesx[j],Coordinatesy[j]);
         if(google.maps.geometry.poly.containsLocation(point,poly)){
             marker2 = new google.maps.Marker({

                 position: point,
                 map: map,
                 icon: 'images/point2.png',

             });
         }
     }
}   

You can see this, by viewing the page's html source.

