Solved

Pass a value from javascript to c#

Posted on 2007-03-21
6
273 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
  • 3
  • 2
6 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
And after further reading Zvonko's reply, I see her mentioned the same thing... sorry Zvonko :o)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
No problem :)
0
 

Author Comment

by:infodigger
Comment Utility
Thank you all for your help!
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
You are welcome.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now