locked
get total problem in text grid view from java script RRS feed

  • Question

  • User988538322 posted

    hi hope all you are fine

    this is my code and problem is that total comes 0. here is my code

        function calculate() {
            var txtTotal = 0.00;
            var passed = false;
            var id = 2;
            while (passed == false) {
                var newId = "";
                if (id.toString().length == 1) { newId = "0" + id.toString(); }
                else { newId = id.toString(); }
                var getElement = document.getElementById("ctl00_ContentPlaceHolder1_GridView1_ctl" + newId + "_TextBox1");


                id = id + 1;
                if (getElement == null) {
                    passed = true;
                }
                else {
                    var currentVal = getElement.value.replace(",", ".");
                    txtTotal = MathRound(parseFloat(txtTotal) + parseFloat(currentVal));
                }
            }
            document.getElementById("<%= total.ClientID %>").value = txtTotal.toFixed(2);
        }


        function MathRound(number) {
            var result = Math.round(number * 100) / 100;
            return result;
        }
        </script>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
            DataKeyNames="ProductID" DataSourceID="SqlDataSource1" ForeColor="#333333" >
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
                    ReadOnly="True" SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                <asp:TemplateField HeaderText="UnitPrice" SortExpression="UnitPrice">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" onchange="calculate()" Text='<%# Bind("UnitPrice", "{0:N}") %>' Width="79px"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        Total :-<asp:TextBox ID="total" runat="server" Text="0"></asp:TextBox>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]"></asp:SqlDataSource>


    I want to get total from text box change event

    Please help

    Saturday, May 10, 2014 4:58 AM

Answers

  • User-734925760 posted

    var getElement = document.getElementById("ctl00_ContentPlaceHolder1_GridView1_ctl" + newId + "_TextBox1");
     

    Hi,

    According to your code, I suggest you putting some break points in this row, to check if we get the real value. If here we get the correct value, I do not think the total value will be 0.

    I search an artical about calculating row total in gridview control, please refer to the link below:

    http://www.aspsnippets.com/Articles/Calculate-Row-Total-and-Grand-Total-in-ASPNet-GridView-using-jQuery.aspx

    Hope it's useful for you.

    Best Regards,

    Michelle Ge

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 12, 2014 4:54 AM