Hello forum,
Previously, I was showed how to compute Grand total based on the percentage value of “VAT” textbox. It is working. I then tried another thing; by adding two other tax textboxes (withholding tax and stamp duty). Then I tried to calculate the Grandtotal with the addition of the two textboxes, but it’s not working. Please kindly help.
HTML
<asp:GridView ID="Gridview1" runat="server" Font-Size="x-Small" ShowFooter="True" AutoGenerateColumns="False" OnRowCreated="Gridview1_RowCreated"
class="table table-striped table-condensed table-bordered" Style="max-width: 100%" HeaderStyle-ForeColor="#00003D" Height="50px" HeaderStyle-Height="10px" HeaderStyle-BackColor="#D3D9E5">
<Columns>
<asp:TemplateField HeaderText="ITEM DESCRIPTION" ItemStyle-Width="47%">
<ItemTemplate>
<asp:TextBox ID="textBox1" runat="server" Class="form-control" Width="100%" Font-Size="Small" TextMode="MultiLine" Style="overflow: hidden; resize: none;" oninput="Resize(this)" />
<script type="text/javascript">
function Resize(textbox) {
textbox.style.height = "";
textbox.style.height = Math.min(textbox.scrollHeight, 300) + "px";
}
</script>
</ItemTemplate>
<FooterStyle HorizontalAlign="Left" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Class="btn btn-primary" BackColor="SteelBlue" Font-Size="Small" Text="+ Item" OnClick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QUANTITY" ItemStyle-Width="11%">
<ItemTemplate>
<asp:TextBox CssClass="form-control" ID="txtQuantity" Height="25" Font-Size="Small" runat="server" Width="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RATE" ItemStyle-Width="19%">
<ItemTemplate>
<span class="currency-symbol" style="font-weight: bolder">NGN</span>
<asp:TextBox ID="txtRate" Height="25" runat="server" Width="80%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AMOUNT" ItemStyle-Width="17%">
<ItemTemplate>
<span class="currency-symbol" style="font-weight: bolder">NGN</span>
<asp:Label ID="lblAmount" runat="server" Text="" Font-Bold="True"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" ForeColor="Red" runat="server" OnClick="LinkButton1_Click">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/del6.png" Height="25" />
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle Height="10px" />
</asp:GridView>
<hr />
<div class="total">
<asp:Label ID="Label1" runat="server" Font-Bold="true" Text="TOTAL"></asp:Label>
<asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>
<br /><br />
<div class="row">
<asp:Label ID="Label10" runat="server" Font-Bold="true" Text="WHT:"></asp:Label>
<asp:TextBox ID="Whttxt" runat="server" CssClass="form-control" Width="107px" Font-Bold="true" Height="22px" Font-Size="8pt" placeholder="Withholding Tax"></asp:TextBox>
<asp:Label ID="Label8" runat="server" ForeColor="Red" Font-Size="XX-Small" Font-Bold="true" Text="(optional)"></asp:Label>
</div>
<br />
<div class="row">
<asp:Label ID="Label2" runat="server" Font-Bold="true" Text="SD:"></asp:Label>
<asp:TextBox ID="Sdtxt" runat="server" CssClass="form-control" Width="103px" Font-Bold="true" Height="22px" Font-Size="8pt" placeholder="Stamp Duty"></asp:TextBox>
<asp:Label ID="Label11" runat="server" ForeColor="Red" Font-Size="XX-Small" Font-Bold="true" Text="(optional)"></asp:Label>
</div>
<div class="row">
<asp:Label ID="Label5" runat="server" Font-Bold="true" Text="VAT:"></asp:Label>
<asp:TextBox ID="txtVAT" runat="server" CssClass="form-control" Width="108px" Font-Bold="true" Height="22px" Font-Size="8pt" placeholder="Value Added Tax"></asp:TextBox>
<asp:Label ID="warnlbl" runat="server" ForeColor="Red" Font-Size="Small" Font-Bold="true" Text=""></asp:Label>
</div>
<br />
<asp:Label ID="Label6" runat="server" Font-Bold="true" Text="GRAND TOTAL:"></asp:Label>
<asp:Label ID="lblGrandTotal" runat="server" Text=""></asp:Label>
</div>
JAVASCRIPT
<script type="text/javascript">
$(function () {
CalculateTotal();
});
$("body").on("change keyup", "[id*=txtQuantity],[id*=txtRate],[id*=txtVAT]", function () {
var row = $(this).closest("tr");
//Check whether Quantity value is valid Float number.
var quantity = parseFloat($.trim($(row).find("[id*=txtQuantity]").val()));
if (isNaN(quantity)) {
quantity = 0;
}
var rate = parseFloat($.trim($(row).find("[id*=txtRate]").val()));
if (isNaN(rate)) {
rate = 0;
}
//Calculate and update Row Total.
$("[id*=lblAmount]", row).html(quantity * rate);
CalculateTotal();
});
function CalculateTotal() {
//Calculate and update Grand Total.
var grandTotal = 0;
$("[id*=lblAmount]").each(function () {
var amount = 0;
if (!isNaN($(this).html()) && $(this).html() != "") {
amount = $(this).html();
}
grandTotal = grandTotal + parseFloat(amount);
});
$("[id*=lblTotal]").html(grandTotal);
var vat = parseInt($("[id*=txtVAT]").val()) / 100;
grandTotal = grandTotal + (grandTotal * vat);
$("[id*=lblGrandTotal]").html(grandTotal.toString());
}
</script>