views:

105

answers:

2

Hi, I'm using ASP.NET and a Repeater control to display my data. The data I have is in stored in a List. How would I implement a simple calculator that is affected in real time? Would I have to use AJAX, or is it possible to do it client-side? I'd like the user to be able to change the quantity, with the new price being visible immediately. Seeing as there's a lack of persistence, is there way to prevent constant round-trips to the database to get the price of an item? Thanks

+1  A: 

If possible, you can place the price of the item in hidden fields next to the quantity input. Then use simple javascript to calculate the total. Otherwise you can use DynamicPopulate to populate the total using AJAX. But that is still going to get you round trips. You could also do your own ajax which would cache the prices coming back. But that seems the same as placing them in hidden inputs.

Yuriy Faktorovich
Thanks for the reply. I guess I could store the prices as hidden inputs, but is it possible to kill two birds with one stone? Since I'll be displaying the price anyway, should I have a certain type of control/field in the repeater for the price, instead of simply outputting the text? Or will I still need to output the standard text, as well as having a hidden input?
keyboardP
You could output the text in a div for example, and then calculate based on the div's innerHTML property. But beware of the total produced by the user, and make sure the user entered an int for the quantity(to avoid him entering .1 and you giving him a 90% discount).
Yuriy Faktorovich
Thanks for the help. I'll go ahead and use that method. There will be checks in place to ensure that the user doesn't change the quantity to a decimal.
keyboardP
A: 

You could do this client side with JavaScript or server side via AJAX or through a postback, it just depends how fluid you want the UI to be. What kind of calculations will you need to do, simply monetary 2 decimal values? I'd be tempted to go for a JavaScript solution with a server side postback fallback solution

To prevent a round trip to the server, you'll need some way of supplying the price value on the client side, whether visible or hidden to the client.

Russ Cam
Thanks for the reply. The calculations will indeed simply be a 2 decimal monetary calculation. It's essentially a view of the user's shopping cart, so they can quickly change the quantity and see how it affects the price for that item. What would be the best visible way of supplying the price client-side?
keyboardP