Real time 'price x quantity' calculator - asp.net

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

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.

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.

Related

Reducing the 'asyncPostBackControlIDs' list

I have a page containing a large amount of controls that can trigger postbacks to the server. I know that it is not considered good practice to have a huge amount controls on one page, but the nature of my ASP.NET web application requires it. Seeing that I have a large amount of controls on this page, I make extensive use of UpdatePanels. I am storing ViewState in Session in order to reduce the response size of the page, which works beautifully.
However, with Fiddler I noticed that the 'asyncPostBackControlIDs' after the '__VIEWSTATE' part of the ajax response contains a list of every control of every UpdatePanel that can trigger a postback. This list is huge! It seems that this list doesn't change much per page, and thus it doesn't make sense to download the entire list every time a postback within an UpdatePanel occurs...
Is there any way to store the 'asyncPostBackControlIDs' on server like you can do with ViewState, or otherwise reduce the size of the 'asyncPostBackControlIDs' list?
AFAIK, there is no documented way for storing asyncPostBackControlIDs else where - further, I am very much in doubt if there can be such possibility because most probably those control ids would be required on client side to decide whether to do regular or asynchronous post-back.
You can set ChildrenAsTriggers property of UpdatePanel to false and register controls manually that are actually triggering the post-back - for example, you may have LinkButton/HyperLink that has java-script handler.
Further, perhaps you may able to reduce the list of controls that can cause post-back. For example, you can use anchors(a) instead of linkbuttons/hyperlinks and then set a hidden variable and then simulate a (hidden) button click. On server side, the value of hidden variable would indicate the actual control responsible for post-back. This way, you can have one button that is a post-back control for many other controls.
Finally, you are really a guy who want to have very efficient request/response streams then abandon ASP.NET control model and rather use ASP.NET MVC. Or at less scale, stop using Update Panels for AJAX (they do complete POST of page and almost entire page cycle is executed at server side) and use script services (along with jquery plugins) instead.
Lastly, you need to see the control ids size with the actual response size - for example, 5K long ids within 100K response may not be a large overhead. Reducing those ids would probably give you 5% savings (if possible) but are efforts for the same worth it?

Listbox items client side reordering not reflected in server side (ASP.NET)

I reordered some items in a listbox using Javascript. When I read the items in a postback in the code behind (ASP.NET), the order is in the original order. How do I get the same order as shown in the screen after Javascript manipulation?
Only the selected items will be posted back to the server, the order will be pulled from the viewstate (which your javascript doesnt change). Im not sure you can do what you want in that way. You might have to have a separate [hidden] field that tells the server what order things are in.
Your JavaScript will have to notify the server of the rearrangement using AJAX, and then you have to keep track of order manually. This is fairly trivial, but it does mean regenerating the listbox items each page load instead of relying on the ViewState.
Using JavaScript code, you can store the order in a hidden variable (make the hidden variable the server HiddenField variable) and process the ordering when the page posts back to the server. Then, you can clear the items and reorder them appropriately using this sequence of numbers stored in the hiddenfield. This is what the AJAX controls do.

How to update every second from db?

I have to build a site like an auction-site:
I Have a detail page from items where a countdown should run down.
In this page nearly every second a update must be possible without a postback for the user:
coundown must be reset
money of aucton must be updated
gridView with last bidders must be updated
What kind of timer should I use for the countdown?
How can I update every second from DB? ( Ajax? )
How can I update the values / gridView?
You can use an UpdatePanel with Ajax.
However, given the nature of "Internet weather," one second per update is pretty aggressive for that approach.
You might instead consider using Silverlight. You would have much more control that way, and could minimize the amount of data that needs to go over the wire. In fact, you could use long polling with raw TCP connections, to further increase scalability.
For the countdown, you can use the JavaScript timing events. To access the database, if you don't want postback then you indeed need to access the data service by using Ajax. To make things easier, I recommend that you take a look at any Javascript libary such as JQuery.
For the timer I would use Threading.Timer so this can count down uninterrupted in a separate thread. You could also use a TimerCallback delegate which would do the database processing. However, I would be wary about trying to query the database at such a rate.
I would advise you look at using Ajax Update Panel for updating the countdown section of the page alone so you don't have to refresh the entire page.

Continuously update an ASP.NET page using AJAX

I have an ASP.NET page that is interacting with a business class. I want to continuously update controls on my page based on user entered values, e.g. update totals. The calculations are embedded in business logic but they're simple enough to reproduce elsewhere. I've thought of three ways to accomplish this:
Dynamically update the page using JavaScript. I don't want to do this because I don't want to risk floating point math problems where the values on the page don't match the values calculated by the business class (those properties are decimals).
Clear calculated fields on changes and force the user to click a re-calculate button. This is a poor user experience and wiring up JavaScript to ASP.NET controls is tedious.
Use an AJAX UpdatePanel, set data entry controls to autopostback, and handle the "changed" event for the control, e.g. TextChanged for TextBox.
The third method seems cleanest to me, provides a nice user experience, and allows me to interact directly with my business class that I've stored in session state.
My question is: Is this a good idea and/or a common practice? Are there better ways to accomplish this?
I haven't done ASP.NET work for a few years and I have a prejudice against autopostback[1]. I've looked at the size of the request and it's currently negligible at 1.5kB. The site will be low use but we may have a small number of users with dial-up connections.
And ASP.NET in general but times are tough.
Personally, I think UpdatePanel is too heavy. You could use jQuery along with an ASP.NET Web service function that outputs JSON.
You're correct in thinking the third option is your best. This is the kind of thing that AJAX is made for. Go for it.

Add and Retrieve Rows aaded to DataGrid using Javascript ASP.Net 1.1

I am able to add a row by using javascript DOM cloneNode method, Now I would like to read all the Rows of the dataGrid on the Server side.
Any Help would be appreciated
This can't be done without resorting to using hidden fields inside your form to store newly added rows. This is because the values of the rows you add to the datagrid are not posted back to the server. You need to store the row values into a hidden form field that will be posted back and you can access the values on the server side and parse it into rows and do whatever with it.
A little more insight in to what you're trying to accomplish might be helpful. Ali already mentioned one way that your code could work. You could have your code add values to hidden form fields along with adding the rows on screen. This would allow you to add rows at the browser level without requiring a post back. Of course, the downside to this is that you will still need a page refresh before your server side code can process any of the additions.
Your second option is to implement some kind of AJAX behavior on the page. This will allow your server side code to access the new row values as they are being added without requiring a total page refresh. Since you're using .NET 1.1 you can't use Microsoft's AJAX toolkit, but you could try Anthem.NET. Never tried it myself, but it's better than hand rolling your own...
http://anthem-dot-net.sourceforge.net

Resources