473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Paasing values to javascript from asp.net

Hey all,

I have some values in the database which has to retrieved and then
passed on to some functions in the javascript. The javascript file is
a separate file. I have included it in the aspx page using the src
option. How do I pass the values that I get from the database to
javascript ? Any help would be appreciated.

Feb 14 '07 #1
5 20373
"Priya" <pr****************@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
I have some values in the database which has to retrieved and then
passed on to some functions in the javascript. The javascript file is
a separate file. I have included it in the aspx page using the src
option. How do I pass the values that I get from the database to
javascript ? Any help would be appreciated.
1) Store the values in hidden fields

or

2) Build up your JavaScript calls dynamically so that you can plug in the
correct parameter values
Feb 14 '07 #2
Remember that JavaScript is just that: script. It is text. Database data is
not text. So, the first thing you need to do is to make sure that you
convert the data to the appropriate scripting text. Second, JavaScript is
scoped to a single HTML document instance, so if your Page is going to
PostBack, you will need to persist the data by passing it back and forth in
the Request, via hidden form fields, ViewState, or server-side state
persistence.

If your JavaScript is in a separate file, you will need to insert a script
into the Page that declares some variables or passes the scripted data to
functions in the external scripting file.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.

"Priya" <pr****************@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Hey all,

I have some values in the database which has to retrieved and then
passed on to some functions in the javascript. The javascript file is
a separate file. I have included it in the aspx page using the src
option. How do I pass the values that I get from the database to
javascript ? Any help would be appreciated.

Feb 14 '07 #3
This may help you too:

http://usableasp.net/DeveloperPage.a...rAndClient.htm

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Priya" <pr****************@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Hey all,

I have some values in the database which has to retrieved and then
passed on to some functions in the javascript. The javascript file is
a separate file. I have included it in the aspx page using the src
option. How do I pass the values that I get from the database to
javascript ? Any help would be appreciated.

Feb 14 '07 #4
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

Feb 14 '07 #5
On Feb 15, 2:51 am, "Todd Gill" <t...@ossia-systems.comwrote:
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 Systemswww.ossia-systems.com
Guys, Thanks for the help extended. It was very useful.

Feb 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
15
by: binnyva | last post by:
Hello Everyone, I have just compleated a JavaScript tutorial and publishing the draft(or the beta version, as I like to call it) for review. This is not open to public yet. The Tutorial is...
10
by: ankisharma | last post by:
Hi all At many places I have seen that programmers pass global variables to functions in c. I am not able to figure out why they do so. need some clues on this. somewhere i heard that this...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
3
by: ishwarbg | last post by:
Hi Everyone, I have a .Net Application, through which I am invoking a function from a legacy DLL developed in C++. My structure in C# contains some data of type double which I need to pass to to...
5
by: kosta.triantafillou | last post by:
Hi all, I have a form that contains a lot of values. On this form there are also alot of popups that can be brought up. One of them does the following: Takes 2 values (x and y), concatenates...
6
Fary4u
by: Fary4u | last post by:
Hi i'm in deadend i don't know how to do the job i'm bit confused ? i'm just thinking how to write the code ? i've got dropdown list of colors & whn i select the colors it's stored into database...
0
by: atencorps | last post by:
Hello I have the following code but need some help on it. The idea of the code is the main sections ie Service Management are viewable when the page is loaded and by clicking on the main...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.