在javascript循环中放置服务器端变量

Place Server side Variable in javascript loop

本文关键字:服务器端 变量 javascript 循环      更新时间:2023-09-26

我有一个服务器端变量,代码如下

 Dim mgps As New Text.StringBuilder
    Public ReadOnly Property GPS() As String
        Get
            Return mgps.ToString
        End Get
    End Property
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim con As New OleDbConnection
        con = New OleDbConnection("Data Source=sml; User ID=sml; Password=sml; provider=OraOLEDB.Oracle")
        con.Open()
        Dim cmd As OleDbCommand = New OleDbCommand("Select STA_NAME, GPS_ONE from GPS", con)
        Dim ds As New DataSet
        Dim I As Long
        Dim da As New OleDbDataAdapter(cmd)
        da.Fill(ds, "GPS")
        mgps.AppendLine("[")
        For I = 0 To ds.Tables("GPS").Rows.Count - 1
            mgps.AppendLine(ds.Tables("GPS").Rows(I).Item("GPS_ONE") & ",")
        Next I
        mgps.AppendLine("];")
    End Sub

全局变量命名为GPS

现在我试图访问它在客户端JS数组JS数组

 var store_locations = new Array();
    store_locations = [ 
       new google.maps.LatLng(31.204549793299,72.264183974237),
        new google.maps.LatLng(31.23004996385,72.282225091978),
        new google.maps.LatLng(31.148218,72.224542),
        new google.maps.LatLng(31.2330555556,72.3330555556)
        ];

现在我想使用GPS变量值而不是store_locations中的自定义值。

这是怎么做到的,谁来帮帮我。

这是我的循环,我想访问数组的值。

 for(i=0; i<store_locations .length-1; i++)
    {
          var image = 'ico/no.png';
          markers[i] = new google.maps.Marker(
          { 
           position: store_locations [i],
           map: map,
           draggable:true,
           icon:image,
           title: (i+1) + GPS
           });                             
    }    

我可以像下面这样访问GPS变量

var GPS = <%=GPS %>

并将store_locations替换为GPS,但my map变为空

请告诉我我到底错过了什么?

您可以创建一个公共属性,在代码开始时返回GPS值。创建StringStringBuilder对象并定义公共属性

Dim mgps As new Text.StringBuilder
Public ReadOnly Property GPS As String
  Get
     Return mgps.ToString
  End Get
End Property

使用mgps (stringbuilder)对象从数据库结果中添加数据

mgps.AppendLine("[")
For I = 0 To ds.Tables("GPS").Rows.Count - 1
    mgps.AppendLine(ds.Tables("GPS").Rows(I).Item("GPS_ONE") & ",")
Next I
mgps.AppendLine("];")
现在你可以在JavaScript中使用GPS了:
 var  GPS = <%=GPS %> 

GPS变量的作用域在for循环内(在服务器端代码中)-因此它不存在于该循环之外。

我不完全确定你想要做什么,因为现在你没有在循环外持久化这些值,但是在循环之前声明GPS变量至少可以让你在当前尝试的地方访问它。

编辑:它应该看起来像这样:

    da.Fill(ds, "GPS")
    Dim GPS As String
    For I = 0 To ds.Tables("GPS").Rows.Count - 1
        I = ds.Tables("GPS").Rows.Count - 1
        GPS = ds.Tables("GPS").Rows(I).Item("GPS_ONE")
        Dim STATION As String = ds.Tables("GPS").Rows(I).Item("STA_NAME")
    Next I

在你的代码后面,你是附加VB行你的StringBuilder返回给客户端。因此,我认为您的客户端渲染代码(在页面加载后执行查看源代码以确认)在javascript中显示:

var GPS1 = 'gps_line_1
gps_line_2
gps_line_3';

更改以下行:

mgps.AppendLine(ds.Tables("GPS").Rows(I).Item("GPS_ONE"))

:

mgps.Append(ds.Tables("GPS").Rows(I).Item("GPS_ONE"))
mgps.Append("'n")

这样,客户端渲染的javascript将是这样的:

var GPS1 = 'gps_line_1'ngps_line_2'ngps_line_3';