473,769 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 20394
"Priya" <pr************ ****@gmail.comw rote in message
news:11******** **************@ l53g2000cwa.goo glegroups.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.comw rote in message
news:11******** **************@ l53g2000cwa.goo glegroups.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.comw rote in message
news:11******** **************@ l53g2000cwa.goo glegroups.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="javas cript">

function myFunctionToRun AfterDB()
{
//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(strT oPrint)
{
alert(strToPrin t);
}
from your asp.net page:

....
<script src="myScript.j s" language="javas cript"></script>
....
<script language="javas cript">
//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.comwrot e:
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="javas cript">

function myFunctionToRun AfterDB()
{
//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(strT oPrint)
{
alert(strToPrin t);
}

from your asp.net page:

...
<script src="myScript.j s" language="javas cript"></script>
...
<script language="javas cript">
//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.ossi a-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
4646
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 month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
15
4230
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 avaliable at... http://www.geocities.com/binnyva/code/javascript/advanced_tutorial/ If any of you could spare the time, please have a look at my tutorial
10
1971
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 philosophy is from object orieted world but is it applicable for c?
41
4976
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 the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
3
2131
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 the DLL to get some results back from it. My Structure In C# looks like this: public struct InputPurchaseOrder { public System.String poJobName;
5
2464
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 them together with an underscore in between and adds them to a select box in the same popup (z). Then, when a user clicks a save button located on the popup, it sends that information back to the main form and populates the same three values (x,...
6
2544
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 color Varibale is ComboName now the only problm is product price is fix ie ( 20$ ) & it's only 3 category ie ( intcat ). but the 1st 3 colors ie ( 1,2,3 ) is 5$ less then product price and next 4 colors ie ( 4,5,6,7 ( is 7$ less then...
0
1972
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 titles/headings or clicking on the 'expand all' will allow you to view the items underneath each title/heading. The page seems to be broken at the Incident and Request Management title/heading section as shown in the image attached. Can anyone help /...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.