Javascript将网格中的值相加并显示在网格的页脚中

时间:2013-05-28 04:25:27

标签: c# .net gridview javascript

大家好,目前我正在开发一个javascript函数,需要在页脚行中显示gridview单元格的总和。现在我能够得到答案,但我无法保存页脚行中的值。         任何人都可以帮助我将值保存到gridview页脚标签。提前谢谢。

这是我的js功能。

    function addTotal() {
        var input = document.getElementsByTagName("input");
        var Total = '0.0';
        var Sample;
        var val;
        for (var i = 0; i < input.length; i++) {
            if (input[i].type == "text") {
                if (input[i].id.indexOf("txtpercent") > 0) {
                    Sample = document.getElementById(input[i].id).value;
                    var val = parseFloat(Sample.replace(",", ""));

                    if (isNaN(val) == true) {
                        val = 0;
                    }

                    Total = parseFloat(Total) + val;
                    //document.getElementById("Flblallocationpercent").innerHTML=Total;
                }
            }
        }
        alert(Total);
        if (Total != 100) {
            alert("Allocation should be equal to 100 %")
            return false;
        }
    }

这是函数调用的设计部分

<asp:TemplateField HeaderText="Allocation Percentage">
                            <ItemTemplate>
                                        <asp:TextBox ID="txtpercent" runat="server" Text="" OnTextChanged="allocate_sum"
                                            Visible="true" ToolTip="Percentage" onblur="addTotal()" />

                            </ItemTemplate>
                            <FooterTemplate >

                                        <asp:Label ID="Flblallocationpercent" runat="server" Enabled="true" Width="95%" />

                            </FooterTemplate>
                            <FooterStyle HorizontalAlign="Center" Width="5%" />
                            <ItemStyle HorizontalAlign="Center" Width="5%" />
                        </asp:TemplateField>

2 个答案:

答案 0 :(得分:0)

参考以下Javascript功能:

for (var i = 1; i < gridLength - 1; i++) {


                    sprice = grid.rows[i].cells[3].innerText;

                    sprice = sprice.replace(code, "");

                    sprice = sprice.replace(",", "");
                    if (sprice == "")
                        price = 0;
                    else
                        price = parseFloat(sprice);


                    subTotal = subTotal + parseFloat(price);

                }

通过THIS链接,您还可以获得有关抗辩的代码。

希望它有所帮助。

答案 1 :(得分:0)

请注意下面提到的代码,将cssclass分配给文本框和标签。

<asp:TemplateField HeaderText="Allocation Percentage">
                            <ItemTemplate>
                                        <asp:TextBox ID="txtpercent" runat="server" Text="" Visible="true" ToolTip="Percentage" CssClass="txtAmount" onblur="addTotal()" />

                            </ItemTemplate>
                            <FooterTemplate >

                                        <asp:Label ID="Flblallocationpercent" runat="server" CssClass="TotalValue" ForeColor="Red" Width="95%" />

                            </FooterTemplate>
                            <FooterStyle HorizontalAlign="Center" Width="5%" />
                            <ItemStyle HorizontalAlign="Center" Width="5%" />
                        </asp:TemplateField>

在您的头部添加以下Javascript功能

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">                     

            function addTotal() {

                var Total = '0.0';
                var Sample;

                $(".txtAmount").each(function(){


            Total += parseFloat($(this).val());
            });

            alert(Total);
            $(".txtAmount").val(Total);
                    //OR
            $(".txtAmount").html(Total);
        }
</script>

并检查它是否解决了您的问题。希望它有所帮助