• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

Pass a value from javascript to c#

I need to retrieve a value calculated using javascript back to my c# code.

The javascript code is here:

      function calculateShipping(){            
            var numberTotal = new NumberFormat( parseFloat(Shipping.value) + parseFloat(subTotal) );
            numberTotal.setPlaces(2);
            
            totalPrice.innerHTML = baseCurrency + numberTotal.toFormatted();
      }

I need to pass the numerTotal back to c# when an imagebutton is clicked on a page.

Thanks!!
0
infodigger
Asked:
infodigger
  • 3
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
That is NOT possible.
C# is running ONCE before the page is displayed to browser on web server side. When the C# has finished and all the output html page source text is flushed to browser, then is the browser starting to render the html page and run any embedded script in the page html text.
The script on the browser page cannot access  ANYTHING on web server side.
All the a browser can do to server is to request a new URL to be loaded into page or submit form data that unloads the actual window content.
The only exception that browser cand do is use hidden techniques, like iframe communication in the background, or AJAX, or <img> or Image() object manipulation that do call another aspx web server page in the background.
So your image click can have an image src attribute set in that way that some aspx page on server side is called to update the Session variables to the new calculated Total.

But why do you want do that at all! At next postback are all form fields submitted anyway. So put your Total in some hidden field and fetch it from there at next post back when you do the next recalculation for that page.



0
 
Cyber-DrugsCommented:
One possible way is with an AJAX Call...

Make AJAX call a page, eg:

www.yoursite.com/getvalue.aspx?myvalue=1234

Where:

"getvalue.aspx" is a c# page to retreive your JS value
"myvalue" is the POST variable to hold your value
"1234" is your POST value

Have AJAX call that page, but do not bother to display it. I do this method with PHP quite often, if you like I can pull out the code I use for this to help you along.

Hope that helps.
0
 
Cyber-DrugsCommented:
And after further reading Zvonko's reply, I see her mentioned the same thing... sorry Zvonko :o)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ZvonkoSystems architectCommented:
No problem :)
0
 
infodiggerAuthor Commented:
Thank you all for your help!
0
 
ZvonkoSystems architectCommented:
You are welcome.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now