asp.net how do I pass the background C # array to the foreground JS?

Source: Internet
Author: User
Tags array length

How to pass the background of C # array to the front-end JS, so this problem plagued for a long time, later in an article to see the solution, the article in the method of processing is fixed-length array, I now deal with is indefinite long, so I have modified on the basis of the article. I personally practice a bit and apply in their own procedures, sure enough to solve the problem. Now combine Daniel's article and my own personal practice to explain how this problem is solved.

   

First step: Define the CS array

CS file in the background program to have an array, this array to define as a public array.

Public string[] lat = null;

Public string[] LNG = NULL;

Step Two: Assign values to the CS array

CS array of values are generally taken from the database, I believe everyone will also, and behind the code will be described, here is not to do a detailed explanation.

Step three: Assign the CS array to the JS array at the front

This step is the key, I choose the method is <%=cs array%>. Such a vague argument is also Baidu, the assignment will be used to the cycle, that is, an element of the assignment of elements.

Background CS Code

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data.OleDb;
Using System.Data;
Using System.Collections;

public partial class VideoSource:System.Web.UI.Page


{


public string[] lat = null;//Store latitude value


Public string[] LNG = null;//Store Longitude value


public int lng_len = 0;//to get array length


public int k = 0;//for Assignment loops


protected void Page_Load (object sender, EventArgs e)


    {


ArrayList lng_list = new ArrayList ();


ArrayList lat_list = new ArrayList ();


OleDbConnection con = new OleDbConnection (@ "Provider=microsoft.ace.oledb.12.0;data source=" + Server.MapPath ("app_data/database1.accdb"));


con. Open ();


String sql = "SELECT * from Tb_videos";


Try


        {


OleDbDataAdapter gh = new OleDbDataAdapter (sql, con);


DataSet ds = new DataSet ();


GH. Fill (DS);


con. Close ();


foreach (DataRow DR in DS. Tables[0]. Rows)


            {


lng_list. ADD (Dr[2]. ToString ());


lat_list. ADD (Dr[3]. ToString ());


}


        }


Catch


        {


con. Dispose ();


        }


LNG = (string[]) lng_list. ToArray (typeof (String));


lat = (string[]) lat_list. ToArray (typeof (String));


Lng_len = lng_list. Count;


    }


ASPX code

<script type= "Text/javascript"
        var Jingdu = new Array ();
        var weidu = new Array ();
        <%
        for (int k=0;k <lng_len;k++) {
        %>
          Jingdu.push ("<%=lng[k]%>");
         Weidu.push ("<%=lat[k]%>");
        <%
       }
         %>
         var latlng=[ ];
         for (var i=0;i<jingdu.length;i++) {
          Latlng.push (New Google.maps.LatLng (jingdu[i],weidu[i));
        }
</script>

The code I used to solve the problem was tested and passed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.