Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Javascript对网格中的值求和,并显示在网格的页脚中_C#_.net_Gridview_Javascript - Fatal编程技术网

C# Javascript对网格中的值求和,并显示在网格的页脚中

C# Javascript对网格中的值求和,并显示在网格的页脚中,c#,.net,gridview,javascript,C#,.net,Gridview,Javascript,大家好,目前我正在开发一个javascript函数,该函数需要在页脚行中显示gridview单元格的总和。现在我可以得到答案,但我无法保存页脚行中的值。 有人能帮我把值保存到gridview页脚标签吗。提前谢谢 这是我的js函数 function addTotal() { var input = document.getElementsByTagName("input"); var Total = '0.0'; var Sample;

大家好,目前我正在开发一个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;
        }
    }
函数addTotal(){
var input=document.getElementsByTagName(“输入”);
风险价值总额='0.0';
var样本;
var-val;
对于(变量i=0;i0){
Sample=document.getElementById(输入[i].id).value;
var val=parseFloat(Sample.replace(“,”,”);
如果(isNaN(val)=真){
val=0;
}
总计=总浮点数(总计)+val;
//document.getElementById(“Flblallocationpercent”).innerHTML=Total;
}
}
}
警报(总数);
如果(总计!=100){
警报(“分配应等于100%”)
返回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>

请参阅以下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);

                }
for(var i=1;i
通过链接,您还将获得代码隐藏引用


希望有帮助。

请注意下面提到的代码,为文本框和标签分配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>

函数addTotal(){
风险价值总额='0.0';
var样本;
$(“.txtAmount”)。每个(函数(){
Total+=parseFloat($(this.val());
});
警报(总数);
美元(“.txtAmount”).val(总计);
//或
$(“.txtAmount”).html(总计);
}

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

您是否确保DOM中有元素Id
Flblallocationpercent
Flblallocationpercent是页脚标签的名称。我不确定它是否有ID元素。Tats为什么我评论Line默认情况下,服务器端控件的ID和名称将相同。只需检查该控件是否在DOM中,并在取消注释该行后检查该页面中是否存在任何javascript错误。。Microsoft JScript运行时错误:“document.getElementById(…)”为null或不是ObjectId,因此表示元素不在
DOM
中。请检查页面源代码并确保元素位于DOM中。一旦它可用,您的代码将很好地工作。不。。它不起作用。该值显示为一些未定义的值(0.Nannan…),但它不显示在页脚中..在哪个位置,该值绑定在网格的页脚中?我为您提供了一个链接,在该链接上,它清楚地显示了到页脚的绑定值