Place Server side Variable in javascript loop

前端 未结 3 385
礼貌的吻别
礼貌的吻别 2020-12-12 06:23

I have a Server side variable by the following code

 Dim mgps As New Text.StringBuilder
    Public ReadOnly Property GPS() As String
        Get
                     


        
相关标签:
3条回答
  • 2020-12-12 07:09

    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
    
    0 讨论(0)
  • 2020-12-12 07:21

    You may create a public property that returns GPS value in code-begind. Create a String or StringBuilder object and define public property.

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

    Use mgps (stringbuilder) object to add data from database result.

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

    Now you can use GPS in JavaScript:

     var  GPS = <%=GPS %> 
    
    0 讨论(0)
  • 2020-12-12 07:23

    In your code-behind you are appending VB lines to your StringBuilder that you return to the client. So I'd imagine your client-side rendered code (do a View Source after page loads to confirm) shows this in the javascript:

    var GPS1 = 'gps_line_1
    gps_line_2
    gps_line_3';
    

    Change the following line:

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

    to:

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

    This way the client-side rendered javascript will be this:

    var GPS1 = 'gps_line_1\ngps_line_2\ngps_line_3';
    
    0 讨论(0)
提交回复
热议问题