?
Solved

Pass a value from javascript to c#

Posted on 2007-03-21
6
Medium Priority
?
294 Views
Last Modified: 2010-08-05
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
Comment
Question by:infodigger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 2000 total points
ID: 18764183
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
 
LVL 4

Expert Comment

by:Cyber-Drugs
ID: 18764349
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
 
LVL 4

Expert Comment

by:Cyber-Drugs
ID: 18764356
And after further reading Zvonko's reply, I see her mentioned the same thing... sorry Zvonko :o)
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 63

Expert Comment

by:Zvonko
ID: 18764493
No problem :)
0
 

Author Comment

by:infodigger
ID: 18764508
Thank you all for your help!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18764569
You are welcome.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question