从.aspx.cs到.aspx中的js代码中传递数据

来源:互联网 发布:windows清理助手 倒闭 编辑:程序博客网 时间:2024/06/12 20:47

原文:关于“Asp.net 中后台CS读取数据库数据生成数组传递给前台页面JS使用”

 

最近,由于项目需要需要将传感器的地理位置信息标记在百度地图上,无线传感器节点能够将自身经纬度信息,通过网络传递到数据库存储起来,然后将其读出来并在百度地图在地图上标记显示出来. 首先,在后台.aspx.cs需要将经纬度读取出来,后台代码如下所示:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<span style="color: rgb(51, 102, 255);">  privatedouble[] Longitudes=newdouble[1024];
    privatedouble[] Latitudes=newdouble[1024];</span>
   <span style="color: rgb(255, 0, 0);">publicstringlongitude=string.Empty;
    publicstringlatitude=string.Empty;</span>
    publicstaticintSumCount;
    protectedvoidPage_Load(objectsender, EventArgs e)
    {
           
          if(!Page.IsPostBack)
          {
              GetLongitude();
              GetLatitude();
          }
            
             
            
    }
    //获取经度
    publicvoidGetLongitude(){
        DBAccess db =newDBAccess();//自己写的连接读取数据库
        SqlDataReader da = db.GetLongitudeData("北京");//获取北京监测站的经纬度
        intcount = 0;
        while(da.Read())
        {
            Longitudes[count] = System.Double.Parse(da["Longitude"].ToString());
           <span style="color: rgb(204, 0, 0);"> longitude += Longitudes[count]+"|";//将读取的传感器经纬度存储为字符串形式</span>
              
            count = count + 1;
        }
        SumCount = count;
    }
    //获取纬度
    publicvoidGetLatitude(){
        DBAccess db =newDBAccess();
        SqlDataReader da = db.GetLongitudeData("北京");//获取北京监测站的经纬度
        intcount = 0;
        while(da.Read())
        {
            Latitudes[count] = System.Double.Parse(da["Latitude"].ToString());
           <span style="color: rgb(255, 0, 0);"> latitude+=Latitudes[count]+"|";//<span style="color: rgb(204, 0, 0);">将读取的传感器经纬度存储为字符串形式</span>
</span>
            count = count + 1;
        }

标记为红色的地方为后来修改加上的,一开始在程序里面就只定义了两个浮点型数组

1
2
<span style="color: rgb(51, 102, 255);">  privatedouble[] Longitudes=newdouble[1024];
   privatedouble[] Latitudes=newdouble[1024];</span>

取完数据以后再前台JS中调用利用ASP的调用语法<%=Longitudes%>和<%=Latitudes%>在前台调用后台生成的数组。当在前台JS调用时,始终出现无法显示数组信息的现象。然后,我跟大家一样GOOGLE查询了许多类似的“Asp.net 后台数组如何传递给前台JS调用”,最后在这篇文章中查找到了类似的方法

http://topic.csdn.net/u/20090609/15/d590347d-4ebc-42ce-b2ae-6afa036b1762.html。所以在代码中加入了标记为红色的代码,将浮点型数组转换成字符串的形式,然后在前台JS写了如下代码:

1
2
3
4
5
6
7
8
9
10
  <span style="color: rgb(204, 0, 0);"> var array1 ='<%=longitude%>';//后台生成的经纬度字符串
     var array2 ='<%=latitude %>';//后台生成的经纬度字符串
     var pointX = array1.split('|');//解析字符串,生成相应的数组
     var pointY = array2.split('|');//解析字符串,生成相应的数组</span>
<span style="color: rgb(204, 0, 0);">      var count = pointX.length;//数组长度</span>
     for(var i = 0; i < count; i++) {
         if(pointX[i] > 0 && pointY[i] > 0) {
             addMarker(new BMap.Point(pointX[i], pointY[i]), i + 3);//向百度地图添加标记
         }
     }

 通过上述转换,就能偶实现后台数组为前台JS所用。虽然网上说的还有许多方法,但小弟时间有限,只实现了这种方法:后台数组转换成字符串,传递给前台,在前台进行解析。希望能偶对大家有帮助,我也是菜鸟,写的不好请大家多多包涵。程序运行如下图所示:读取数据库节点经纬度,在百度地图上

2014-5-31

0 0
原创粉丝点击