Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Passing VB variables to Javascript code

Passing VB variables to Javascript code

Scheduled Pinned Locked Moved ASP.NET
javascriptdatabasetoolstutorialquestion
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    beish1
    wrote on last edited by
    #1

    I have a VB variable in an aspx page and want to pass its value to a javascript function within the same page. Any idea how to do this? The variables are read from a database thus: Dim Lat As Double = CDbl(r("PlaceLat")) Dim Lng As Double = CDbl(r("PlaceLng")) and I would like them to be recognised by a google maps javascript line thus: . . var point = new GLatLng(Lat,Lng); . . <\script> </x-turndown>

    P M R C G 5 Replies Last reply
    0
    • B beish1

      I have a VB variable in an aspx page and want to pass its value to a javascript function within the same page. Any idea how to do this? The variables are read from a database thus: Dim Lat As Double = CDbl(r("PlaceLat")) Dim Lng As Double = CDbl(r("PlaceLng")) and I would like them to be recognised by a google maps javascript line thus: . . var point = new GLatLng(Lat,Lng); . . <\script> </x-turndown>

      P Offline
      P Offline
      peacefulmember
      wrote on last edited by
      #2

      I did it this way after reading variables from database. Do not know if there is another better way.Dim jsScript As String jsScript = "jsVar = '" & VBVar & "';" Dim cs As ClientScriptManager = Page.ClientScript If (Not cs.IsStartupScriptRegistered("InitJSVar")) Then cs.RegisterStartupScript(Page.GetType(), "InitJSVar", jsScript, True) End If

      B 2 Replies Last reply
      0
      • P peacefulmember

        I did it this way after reading variables from database. Do not know if there is another better way.Dim jsScript As String jsScript = "jsVar = '" & VBVar & "';" Dim cs As ClientScriptManager = Page.ClientScript If (Not cs.IsStartupScriptRegistered("InitJSVar")) Then cs.RegisterStartupScript(Page.GetType(), "InitJSVar", jsScript, True) End If

        B Offline
        B Offline
        beish1
        wrote on last edited by
        #3

        Thanks for this. By doing this code, am I making jsVar available to any javascript on the page so that I can have: var point = new GLatLng(jsVar,jsVar2); or do I need to put all the code in the jsScript : jsScript="" & vbCrLF jsScript=jsScript & "jsVar = '" & Lat & "';" jsScript=jsScript & "jsVar2 = '" & Lng & "';" jsScript=jsScript & "var point = new GLatLng(jsVar,jsVar2);" jsScript=jsScript & "<\script>" Will the javascript then run where this is placed: Dim cs As ClientScriptManager = Page.ClientScript If (Not cs.IsStartupScriptRegistered("InitJSVar")) Then cs.RegisterStartupScript(Page.GetType(), "InitJSVar", jsScript, True) End If </x-turndown>

        P 1 Reply Last reply
        0
        • B beish1

          Thanks for this. By doing this code, am I making jsVar available to any javascript on the page so that I can have: var point = new GLatLng(jsVar,jsVar2); or do I need to put all the code in the jsScript : jsScript="" & vbCrLF jsScript=jsScript & "jsVar = '" & Lat & "';" jsScript=jsScript & "jsVar2 = '" & Lng & "';" jsScript=jsScript & "var point = new GLatLng(jsVar,jsVar2);" jsScript=jsScript & "<\script>" Will the javascript then run where this is placed: Dim cs As ClientScriptManager = Page.ClientScript If (Not cs.IsStartupScriptRegistered("InitJSVar")) Then cs.RegisterStartupScript(Page.GetType(), "InitJSVar", jsScript, True) End If </x-turndown>

          P Offline
          P Offline
          peacefulmember
          wrote on last edited by
          #4

          Yes, this way jsVar will be availeble on the page, and can be accessed in javascript. Since, this variable is declared on page so, its scope will be page. In case you have two javascript variables that you want to initialized based on you database value, you will have to follow second approach. By doing this you are generating javascript code on the server side that will be available on the page once loaded. HTH

          1 Reply Last reply
          0
          • B beish1

            I have a VB variable in an aspx page and want to pass its value to a javascript function within the same page. Any idea how to do this? The variables are read from a database thus: Dim Lat As Double = CDbl(r("PlaceLat")) Dim Lng As Double = CDbl(r("PlaceLng")) and I would like them to be recognised by a google maps javascript line thus: . . var point = new GLatLng(Lat,Lng); . . <\script> </x-turndown>

            M Offline
            M Offline
            Michael Sync
            wrote on last edited by
            #5

            As I said here ~

            you can use server-side hidden field to communicate between client-side and server-side. <input id="Hidden1" type="hidden" runat="server"/> In Form_load protected void Page_Load(object sender, EventArgs e) { Hidden1.Value = "13"; } You can set like that.. In Client-side ~ <script language="javascript" type="text/javascript"> function getvalue(){ var var1 = document.getElementById('Hidden1').value; //do something. } </script>

            Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

            B 1 Reply Last reply
            0
            • B beish1

              I have a VB variable in an aspx page and want to pass its value to a javascript function within the same page. Any idea how to do this? The variables are read from a database thus: Dim Lat As Double = CDbl(r("PlaceLat")) Dim Lng As Double = CDbl(r("PlaceLng")) and I would like them to be recognised by a google maps javascript line thus: . . var point = new GLatLng(Lat,Lng); . . <\script> </x-turndown>

              R Offline
              R Offline
              Rama Krishna Vavilala
              wrote on last edited by
              #6

              Another Easy option might be to use inline tags. Some people like it some hate it. If you feel that it is cleaner than the other options you can do something like this.

              B 1 Reply Last reply
              0
              • B beish1

                I have a VB variable in an aspx page and want to pass its value to a javascript function within the same page. Any idea how to do this? The variables are read from a database thus: Dim Lat As Double = CDbl(r("PlaceLat")) Dim Lng As Double = CDbl(r("PlaceLng")) and I would like them to be recognised by a google maps javascript line thus: . . var point = new GLatLng(Lat,Lng); . . <\script> </x-turndown>

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                For what it's worth, I believe there's an article somewhere on CP where someone wrapped all this google maps stuff into a control which would do all of this. I didn't actually use it, so it may relate to an older version of the API, but I am pretty sure it exists, it's worth looking into.

                Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                1 Reply Last reply
                0
                • B beish1

                  I have a VB variable in an aspx page and want to pass its value to a javascript function within the same page. Any idea how to do this? The variables are read from a database thus: Dim Lat As Double = CDbl(r("PlaceLat")) Dim Lng As Double = CDbl(r("PlaceLng")) and I would like them to be recognised by a google maps javascript line thus: . . var point = new GLatLng(Lat,Lng); . . <\script> </x-turndown>

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  You already have a thread about this[^] that you created only a few hours ago, where I already have shown you how to do this. Please keep it in a single thread.

                  --- single minded; short sighted; long gone;

                  B 1 Reply Last reply
                  0
                  • P peacefulmember

                    I did it this way after reading variables from database. Do not know if there is another better way.Dim jsScript As String jsScript = "jsVar = '" & VBVar & "';" Dim cs As ClientScriptManager = Page.ClientScript If (Not cs.IsStartupScriptRegistered("InitJSVar")) Then cs.RegisterStartupScript(Page.GetType(), "InitJSVar", jsScript, True) End If

                    B Offline
                    B Offline
                    beish1
                    wrote on last edited by
                    #9

                    This worked perfectly. As I was writing away a marker for each database record read, I had to make the key (InitJSVar) a string so that I could change the key each time a new record was read ie first record = InitJSVar1, second record= InitJSVar2 ...etc Many thanks Will give you 5 rating

                    1 Reply Last reply
                    0
                    • G Guffa

                      You already have a thread about this[^] that you created only a few hours ago, where I already have shown you how to do this. Please keep it in a single thread.

                      --- single minded; short sighted; long gone;

                      B Offline
                      B Offline
                      beish1
                      wrote on last edited by
                      #10

                      Thanks for taking the time to assist me. I ended up using peacefulMember's solution

                      G 1 Reply Last reply
                      0
                      • M Michael Sync

                        As I said here ~

                        you can use server-side hidden field to communicate between client-side and server-side. <input id="Hidden1" type="hidden" runat="server"/> In Form_load protected void Page_Load(object sender, EventArgs e) { Hidden1.Value = "13"; } You can set like that.. In Client-side ~ <script language="javascript" type="text/javascript"> function getvalue(){ var var1 = document.getElementById('Hidden1').value; //do something. } </script>

                        Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

                        B Offline
                        B Offline
                        beish1
                        wrote on last edited by
                        #11

                        Thanks for assisting me - I ened up using peacefulmember's solution

                        1 Reply Last reply
                        0
                        • R Rama Krishna Vavilala

                          Another Easy option might be to use inline tags. Some people like it some hate it. If you feel that it is cleaner than the other options you can do something like this.

                          B Offline
                          B Offline
                          beish1
                          wrote on last edited by
                          #12

                          Thanks for your help - I ended up using peacefulmember's solution

                          1 Reply Last reply
                          0
                          • B beish1

                            Thanks for taking the time to assist me. I ended up using peacefulMember's solution

                            G Offline
                            G Offline
                            Guffa
                            wrote on last edited by
                            #13

                            You should specify the format when you convert the value to a string. His solution only works for floating point numbers if the current culture happens to use period for decimal separator.

                            --- single minded; short sighted; long gone;

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups