Paasing values to javascript from asp.net

Discussion in 'ASP .Net' started by Priya, Feb 14, 2007.

  1. Priya

    Priya Guest

    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.
     
    Priya, Feb 14, 2007
    #1
    1. Advertising

  2. Priya

    Mark Rae Guest

    "Priya" <> wrote in message
    news:...

    > 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
     
    Mark Rae, Feb 14, 2007
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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.
    >
     
    Kevin Spencer, Feb 14, 2007
    #3
  4. This may help you too:

    http://usableasp.net/DeveloperPage....o/HowToPassMessagesBetweenServerAndClient.htm

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Priya" <> wrote in message
    news:...
    > 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.
    >
     
    Eliyahu Goldin, Feb 14, 2007
    #4
  5. Priya

    Todd Gill Guest

    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
     
    Todd Gill, Feb 14, 2007
    #5
  6. Priya

    Priya Guest

    On Feb 15, 2:51 am, "Todd Gill" <> wrote:
    > 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.
     
    Priya, Feb 19, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Virgil Hobbs
    Replies:
    0
    Views:
    533
    David Virgil Hobbs
    Jan 6, 2004
  2. =?Utf-8?B?cGhpbA==?=

    JavaScript return values accessible to ASP.NET

    =?Utf-8?B?cGhpbA==?=, Apr 8, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    9,751
    =?Utf-8?B?cGhpbA==?=
    Apr 8, 2005
  3. David Adams
    Replies:
    1
    Views:
    3,724
    =?Utf-8?B?RGFsZQ==?=
    Apr 14, 2006
  4. Replies:
    10
    Views:
    605
    William Hughes
    Jul 29, 2005
  5. avinrosh
    Replies:
    0
    Views:
    406
    avinrosh
    Oct 10, 2007
Loading...

Share This Page