Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
$(function () {
           $("[id*=txtQuantity]").val("0");
           $('[id*=btn1], [id*=btn2]').on('click', function () {
               var value = 0;
               if ($(this).attr('class') == 'Minus') {
                   value = parseInt($(this).closest('tr').find('[id*=txtQuantity]').val()) - 1;
               }
               if ($(this).attr('class') == 'Plus') {
                   value = parseInt($(this).closest('tr').find('[id*=txtQuantity]').val()) + 1;
               }
               if (value < 0) {
                   value = 0;
               }
               $(this).closest('tr').find('[id*=txtQuantity]').val(value);
               CalculateTotal($(this).closest('tr').find('[id*=txtQuantity]'));
               return false;
           });
       });

       $("body").on("change keyup", "[id*=txtQuantity]", function () {
           CalculateTotal($(this));
       });

       function CalculateTotal(ele) {
           //Check whether Quantity value is valid Float number.
           var quantity = parseFloat($.trim($(ele).val()));
           if (isNaN(quantity)) {
               quantity = 0;
           }

           //Update the Quantity TextBox.
           $(ele).val(quantity);

           //Calculate and update Row Total.
           var row = $(ele).closest("tr");
           $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(ele).val()));

           //Calculate and update Grand Total.
           var grandTotal = 0;
           $("[id*=lblTotal]").each(function () {
               grandTotal = grandTotal + parseFloat($(this).html());
           });
           $("[id*=lblGrandTotal]").html(grandTotal.toString());

           var isTotal = $("[id*=lblGrandTotal]").html();
           document.getElementById('TxtGrandTotal').value = isTotal;
           /*alert(document.getElementById('TxtGrandTotal').value);*/
       }


i have the above code which helps me to calculate the row total cost when i click the plus/minus quantity button.

My problem is i cant get the lblTotal total value in code behind, it give me 0 always. I can get the quantity value but not the calculated row total value.


<asp:button id="btn1" runat="server" text=" - " width="25" cssclass="Minus" style="border-radius:25%;font-weight:700;background-color:red" font-size="Large">
<asp:textbox id="txtQuantity" runat="server" width="50" style="text-align: center" visible="true" enabled="true">
<asp:button id="btn2" runat="server" width="25" text=" + " cssclass="Plus" style="border-radius:25%;font-weight:700;background-color:green" font-size="Large">
 
 
 
RM:
<asp:label id="lblTotal" runat="server" text="0" style="text-align: center; font-weight: 700" visible="true" enabled="true">

MyControl in Gridview is above.

What I have tried:

I tried to edit the above js but im screwing up the whole thing
Posted
Updated 10-Oct-21 22:50pm
v2

1 solution

The only values which are sent back to the server are those contributed by enabled <input>, <select>, <textarea>, and <button> controls, as defined by the HTML specification[^].

The ASP.NET WebForms controls which need to submit information back to the server will generate one or more of these in the rendered HTML output.

Based on your ID prefixes, you're using an <asp:Label>. This does not submit any information back to the server. It simply renders a <span>, which does not contribute to the payload of the form.

You have several options:

You could replace the <asp:Label> with an <asp:TextBox>, and set it to "read-only". You would potentially need to apply CSS styles to make it look like you want.

You could intercept the form's formdata event[^] and add the relevant value(s) to the submitted data. You would then need to read the values directly from the Request.Form collection on the server.

Or you could re-calculate the total on the server, using the prices loaded directly from the database. This would be the safest option, as it would prevent malicious users from modifying your form submission to reduce the price you charge them.
 
Share this answer
 
Comments
Member 15347263 11-Oct-21 5:01am    
Thank you so much for the reply, when i use text box i cant calculate the total after quantity changes.
Richard Deeming 11-Oct-21 5:02am    
Yes you can. In the Javascript, you just need to use .val(grandTotal.toString()) instead of .html(grandTotal.toString())
Member 15347263 11-Oct-21 5:46am    
Thank you so much, it is working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900