You should keep in mind at what point the server code is executed by
the server (asp.net), and at what point the javascript code is
executed by the user's browser (javscript).
There are many ways to get your retrieved database values to your
javascript code. If you can include the javascript code on the
ASP.NET page you're using, it can work like this:
1. Update the javascript code when ASP.NET renders the page
<script language="javascript">
function myFunctionToRunAfterDB()
{
//Use ASP.NET to place your value into javascript code
var dbResultString = <%= strDBResult %>;
}
</script>
In this case, of course strDBResult is a public string for this
ASP.NET page.
2. Pass parameters to the javascript function defined in the
static .js file
If the javascript function you are using is in a static file (such as
myScript.js), a similar method may work as long as the javascript
function accepts parameters for the functions you wish to call (or the
javascript function otherwise has scope):
from myScript.js:
....
function myFunction(strToPrint)
{
alert(strToPrint);
}
from your asp.net page:
....
<script src="myScript.js" language="javascript"></script>
....
<script language="javascript">
//call the function, but pass in the newly-retrieved value
from ASP.NET
// this can also be called from any Javscript event such as
a button's "onClick" event
myFunction(<%= strDBResult %>);
</script>
3. Convert the .js file to an ASP.NET page
In some cases, it may be appropriate to convert your .js file into
a .aspx file. The file would still contain javascript code, but data
from ASP.NET can be added using the same technique I described in item
#1.
I've used all three methods with success.
Again, the key is to keep track of when each "layer" of code is being
executed: ASP.NET at the server; javscript at the client.
Todd Gill
Ossia Systems
www.ossia-systems.com