Calculate ASP.Net GridView Running and Grand Total using JavaScript

Last Reply 7 months ago By dharmendr

Posted 7 months ago

How to get quantity multiply price in amount textbox in asp.net.

I work on web application web form asp.net AND I need to get qunatity * price and result display on Amount  

<asp:Label ID="Cart" runat="server" Font-Size="X-Large" Text="Your Cart"></asp:Label>
<br />
<asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False" OnRowCommand="myGridView_RowCommand"
    OnRowDataBound="myGridView_RowDataBound" BackColor="#CCCCCC" BorderColor="#999999"
    BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" CssClass="auto-style2"
    ForeColor="Black" Height="135px" Width="711px">
    <Columns>
        <asp:TemplateField HeaderText="ProductID">
            <ItemTemplate>
                <asp:Label ID="lblPrice" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ProductName">
            <ItemTemplate>
                <asp:Label ID="lblProductName" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Price">
            <ItemTemplate>
                <asp:Label ID="lblProductID" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:TextBox ID="txtQty" runat="server" onkeyup="CalculateTotals();"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Delete">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDelete" runat="server"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
    <RowStyle BackColor="White" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:Label ID="lblSumTotal" runat="server"></asp:Label>

 

<script type="text/javascript">
    function CalculateTotals() {
        var gv = document.getElementById("<%= myGridView.ClientID %>");
        var tb = gv.getElementsByTagName("input");
        var lb = gv.getElementsByTagName("span");

        var sub = 0;
        var total = 0;
        var indexQ = 1;
        var indexP = 0;

        for (var i = 0; i < tb.length; i++) {
            if (tb[i].type == "text") {
                sub = parseFloat(lb[indexP].innerHTML) * parseFloat(tb[i].value);
                if (isNaN(sub)) {
                    lb[i + indexQ].innerHTML = "";
                    sub = 0;
                } else {
                    lb[i + indexQ].innerHTML = sub;
                }
                indexQ++;
                indexP = indexP + 2;
                total += parseFloat(sub);
            }
        }
        lb[lb.length - 1].innerHTML = total;
    } 
</script>

 

This question does not have replies that have been liked.