Solved

Pass a value from javascript to c#

Posted on 2007-03-21
6
276 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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'…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

932 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

8 Experts available now in Live!

Get 1:1 Help Now