Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
949 views
in Technique[技术] by (71.8m points)

vb.net - Place Server side Variable in javascript loop

I have a Server side variable by the following code

 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

The Variable is Global named GPS

Now I am trying to access it in client side JS Array JS Array is below

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

Now I want to use GPS varible values instead of custome values in store_locations.

How can it be done anyone please help me.

Here is my loop where I want to access the array values.

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

I have access the GPS variable as below

var GPS = <%=GPS %>

and replace the store_locations with GPS but my map become empty.

Please tell me what exactly i am missing to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The scope of your GPS variable is inside the for loop (in your server side code) - therefore it does not exist outside of that.

I'm not entirely certain what you are trying to do, because right now you are not persisting the values outside of the loop, but declaring the GPS variable before your loop will at least allow you to gain access to it where you are currently trying to.

EDIT: It should wind up looking something like this:

    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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...