The background C # array interacts with the foreground JavaScript array in ASP.

Source: Internet
Author: User

Excerpt from: http://blog.csdn.net/a6225301/article/details/20003305

In the previous article, "ASP. JavaScript in the background C # interaction" implements the data for the front-end scripting JavaScript call database in the background. But new problems arise, because there are multiple points on the map, so the longitude and latitude store Google Maps have multiple values, which requires the use of arrays to store latitude and longitude, the problem has evolved into the background of the C # array to the front-end JS. Since the first contact with the ASP and the front-end block, basic is 0 basis, so this problem has been plagued for a long time, online search data, later in an article to see the solution, but the method in the article to deal with the fixed-length array, and I now have to deal with a variable length array, because users may add and delete locations on the map, so the records from the database query The number is unknown, so I modified it on the basis of the article. Self-practice and apply it in their own programs, sure enough to solve the problem. Now combine Daniel's article and my own hands-on practice to illustrate how this problem is solved.

The first step: Define the CS array cs file in the background program to have an array, this array to be defined as a common array.

Public string[] lat = null;  

The second step: to the CS array value CS Array values are generally taken from the database, I believe you will also, and the code behind will also have a description, here do not do a detailed explanation.

The third step: Assign the CS array to the front-end JS Array This step is the key, I choose the method is <%=cs array%>. Such vague statement is also obtained by Baidu, the assignment will be used in the loop, that is, an element of the value of the assignment.

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 in T Lng_len = 0;//used to get the array length public int k = 0;//used for Assignment loop protected void Page_Load (object sender, EventArgs e) {A       Rraylist 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 above is for me to solve the problem of the code, have been tested.

The code above is for me to solve the problem of the code, have been tested.

The background C # array interacts with the foreground JavaScript array in ASP.

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.