动态显示gridview某列的和

13 篇文章 0 订阅

这几天遇到这样一个需求,列表显示如下:

 

要求输入费用的同时,右下角动态显示当前费用总和。

 

最后一行是在绑定列表时拼接的一行,代码如下:

 if (dv.Table.Rows.Count > 0)
            {
                int count = this.lpdgPlanFeeList.Rows.Count;
               
                 this.lpdgPlanFeeList.Rows[count - 1].Cells[0].Text = "借用总额: " + ds.Tables["RSumFee"].Rows[0]["SumPlanFee"].ToString() + "  元";
                 this.lpdgPlanFeeList.Rows[count - 1].Cells[0].ID = "totalFee";
                this.lpdgPlanFeeList.Rows[count - 1].Cells[1].Visible = false;
                this.lpdgPlanFeeList.Rows[count - 1].Cells[2].Visible = false;
                this.lpdgPlanFeeList.Rows[count - 1].Cells[3].Visible = false;
                this.lpdgPlanFeeList.Rows[count - 1].Cells[4].Visible = false;
              

                this.lpdgPlanFeeList.Rows[count - 1].Cells[0].ColumnSpan = lpdgPlanFeeList.Columns.Count+1;
                this.lpdgPlanFeeList.Rows[count - 1].Cells[0].HorizontalAlign = HorizontalAlign.Right;
                this.lpdgPlanFeeList.Rows[count - 1].Cells[0].ForeColor = System.Drawing.Color.Red;
            }

 

源文件中显示如下:

 

该列表前台代码如下:

<asp:TemplateField HeaderText="预计费用(元)" ItemStyle-HorizontalAlign="Center">
                        <HeaderTemplate>
                            预计费用(元)<span style="color: Red;">*</span>
                        </HeaderTemplate>
                        <ItemStyle Width="15%" HorizontalAlign="Center" />
                        <ItemTemplate>
                            <asp:TextBox ID="tbPlanFee" runat="server" class="kpms-textbox-money" EnableTheming="false"
                                Text='<%#Eval("PlanFee","{0:F2}") %>' CausesValidation="False" οnchange="return TotaleFee();"></asp:TextBox>
                            <asp:Label ID="lbParamFeeTypeID" runat="server" Visible="false" Text='<%#Eval("ParamFeeTypeID")%>'></asp:Label>
                            <asp:RegularExpressionValidator ID="revtbPlanFee" runat="server" ErrorMessage="费用必须为数字"
                                ControlToValidate="tbPlanFee" ValidationExpression="^([0-9]{0,6})(\.\d{1,6})?$">*</asp:RegularExpressionValidator>
                        </ItemTemplate>
                    </asp:TemplateField>

 

当费用信息改变时调用js函数:

<script type="text/javascript" language="javascript">
    <script type="text/javascript" language="javascript">
    function TotaleFee() {
        var i = 0;
        var j = 0;
        var totalFee = 0.0;
        var feeList = document.getElementById("<%=lpdgPlanFeeList.ClientID %>");
        var tag = feeList.getElementsByTagName("input");
        while (i < tag.length) {
            if (tag[i].type == "text") {
                if (tag[i].id.indexOf("tbPlanFee") > 0) {

                    if (parseFloat(tag[i].value)>0) {      //若不添加此判断,则当有些行的费用列为空时,费用总和显示为NaN
                        totalFee += parseFloat(tag[i].value);
                    }

                }
            }
            i++;
        }
        var tagtd = feeList.getElementsByTagName("td");
        while (j < tagtd.length) {
            if (tagtd[j].id.indexOf("totalFee") > 0) {
               
                tagtd[j].innerText = "借用总额: " + totalFee + "  元";
               
            }
            j++;
        }
    }
</script></script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值