Passing VB variables to Javascript code
-
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>
-
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>
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
-
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
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>
-
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>
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
-
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>
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_loadprotected 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. :)
-
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>
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.
-
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>
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 )
-
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>
-
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
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
-
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;
-
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_loadprotected 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. :)
-
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.