将服务器端变量放入javascript循环中

将服务器端变量放入javascript循环中,javascript,vb.net,google-maps-api-3,Javascript,Vb.net,Google Maps Api 3,我有一个服务器端变量,代码如下 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.Event

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

 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可变值,而不是商店位置的自定义值

这怎么办?请帮助我

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

 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
           });                             
    }    

for(i=0;iGPS变量的范围在for循环内(在服务器端代码中)-因此它不存在于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

您可以创建一个公共属性,该属性在code begind中返回GPS值。创建
String
StringBuilder
对象并定义公共属性

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 %> 
var-GPS=

在您的代码背后,您正在向您的StringBuilder添加VB行,然后返回到客户端。因此,我可以想象您的客户端呈现代码(在页面加载后查看源代码以确认)在javascript中显示:

var GPS1 = 'gps_line_1
gps_line_2
gps_line_3';
更改以下行:

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

这样,客户端呈现的javascript将如下所示:

var GPS1 = 'gps_line_1\ngps_line_2\ngps_line_3';

先生,通过您的代码,GPS只返回循环的最后一个值。我如何访问循环之外的所有循环值?这是因为您的代码Adeel…您的迭代器…下一个循环(“我”在本例中)将从0变为Rows.Count-1值。然后有一行覆盖该值并将其设置为Rows.Count-1。为什么在原始帖子中有这一行?For…Next循环会在每次迭代时自动增加I的值。它如何附加任何示例?它给出的坐标值存储在Db中您可以在这里查看。先生,请回答我的问题。我一直在努力解决这个问题,但地图仍然是空的。我知道这个问题和您的其他问题类似,但您发布的代码不同。我的答案基于您在“将Vb.net循环变量值分配给javascript数组”问题中发布的代码。现在是“将”如果将它们结合在一起,则此答案不适用于此问题的代码。
mgps.Append(ds.Tables("GPS").Rows(I).Item("GPS_ONE"))
mgps.Append("\n")
var GPS1 = 'gps_line_1\ngps_line_2\ngps_line_3';