Get Running Total of TextBox Values in ASP.NET

Currently I am working in health care domain, In that i got the requirement to implement the functionality of getting running total of Service price (which is used by patient) in textbox as soon as they are entered. Whenever we enter the price of any Service in the textbox, total price gets updated in the bottom of the Textbox reflecting the Total Price.

To achieve that just we need to assign the CssClass (e.g. "calculate" in this article) to each textbox that will participate in running total. Then iterate through each textbox having class "calculate" and get the value of the textbox on keyup event and calculate the total and show in label or textbox.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RunningTotalofTextBox.aspx.cs"  
  2.     Inherits="CsharpCornerExamples.Application.RunningTotalofTextBox" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <script src="../JScripts/jquery-1.2.1.min.js" type="text/javascript"></script>  
  9.     <script type="text/javascript">  
  10.         $(document).ready(function () {  
  11.             $(".calculate").each(function () {  
  12.                 $(this).keyup(function () {  
  13.                     var total = 0;  
  14.                     $(".calculate").each(function () {  
  15.                         if (!isNaN(this.value) && this.value.length != 0) {  
  16.                             total += parseFloat(this.value);  
  17.                         }  
  18.                     });  
  19.                     $('#<%=txtTotalPrice.ClientID %>').val(total.toFixed(2));  
  20.                 });  
  21.             });  
  22.         });  
  23.     </script>  
  24.  </head>  
  25. <body>  
  26.     <form id="form1" runat="server">  
  27.     <div>  
  28.         <table border="1px">  
  29.             <tr style="text-align: left;">  
  30.                 <th>  
  31.                     Book  
  32.                 </th>  
  33.                 <th>  
  34.                     Price  
  35.                 </th>  
  36.             </tr>  
  37.             <tr>  
  38.                 <td>  
  39.                     C#.Net:  
  40.                 </td>  
  41.                 <td>  
  42.                     <asp:TextBox ID="txtCshrp" CssClass="calculate" runat="server"></asp:TextBox>  
  43.                 </td>  
  44.             </tr>  
  45.             <tr>  
  46.                 <td>  
  47.                     Asp.Net:  
  48.                 </td>  
  49.                 <td>  
  50.                     <asp:TextBox ID="txtAsp" CssClass="calculate" runat="server"></asp:TextBox>  
  51.                 </td>  
  52.             </tr>  
  53.             <tr>  
  54.                 <td>  
  55.                     Sql Server:  
  56.                 </td>  
  57.                 <td>  
  58.                     <asp:TextBox ID="txtSqlServer" CssClass="calculate" runat="server"></asp:TextBox>  
  59.                 </td>  
  60.             </tr>  
  61.             <tr>  
  62.                 <td>  
  63.                     JQuery:  
  64.                 </td>  
  65.                 <td>  
  66.                     <asp:TextBox ID="txtJQuery " CssClass="calculate" runat="server"></asp:TextBox>  
  67.                 </td>  
  68.             </tr>  
  69.             <tr>  
  70.                 <td>  
  71.                     <b>Total Price</b>  
  72.                 </td>  
  73.                 <td>  
  74.                     <asp:TextBox ID="txtTotalPrice" runat="server"></asp:TextBox>  
  75.                 </td>  
  76.             </tr>  
  77.         </table>  
  78.     </div>  
  79.     </form>  
  80. </body>  
  81. </html>  
Output

output

I hope you enjoyed it.