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

Source: Internet
Author: User

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;//used to get array lengths
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.