asp.net repeater统计表某列值

本文介绍四种使用不同技术(ASP.NET服务器控件事件、JavaScript/jQuery、DOM操作及数据绑定)实现Repeater控件中数据统计的方法,适用于不同场景下的数据汇总需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方式一

decimal sum = 0;  

  protected void RepMain_ItemDataBound(object sender, RepeaterItemEventArgs e)  

  {  

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)  

    {  

    string num = DataBinder.Eval(e.Item.DataItem, "DXFY").ToString().Trim();  

    if (!num.Equals(""))  

            sum += Convert.ToDecimal(num);  

     }  

            this.lblSum.Text = sum.ToString() + "元";  

 }  

 

方式二

<script type="text/javascript">              
        $(function () {              
            StatSum("lblAAcount", "spAAcount");  
            StatSum("lblBBcount", "spBBcount");  
        }  
        function StatSum(lblId, spId) {  
            var sum = 0;  
            $("span[id$='" + lblId +"']").each(function () {  
                sum += parseInt($(this).text());  
            });  
            $("#" + spId).text(sum);  
        }  
</script>  
<asp:Repeater ID="rptTest" runat="server" >  
   <HeaderTemplate>  
     <table>                                  
       <tr>  
        <th>年度</th>                                     
        <th>AA总人数</th>  
        <th>BB总人数</th>                                      
       </tr>  
   </HeaderTemplate>  
   <ItemTemplate>  
      <tr>  
       <td><%#Eval("year")%></td>  
       <td><asp:Label runat="server" ID="lblAAcount" Text='<%#Eval("AAcount")%>'></asp:Label></td>  
       <td><asp:Label runat="server" ID="lblBBcount" Text='<%#Eval("BBcount")%>'></asp:Label></td>  
      </tr>  
   </ItemTemplate>  
   <FooterTemplate>  
      <tr>  
        <td>合计</td>  
        <td><span id="spAAcount"></span></td>  
        <td><span id="spBBcount"></span></td>  
      </tr>  
     </table>  
    </FooterTemplate>  
</asp:Repeater>

 

方式三

 
如何使用js对Repeater生成的table进行列和统计?
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse; width:400px;">
  <tr align="center" style="height:25px;">
  <th style="width:50px; line-height:120%;">序号</th>
  <th style="width:150px; line-height:120%;">样文</th>
  <th style="width:200px; line-height:120%;">录入列</th>
  </tr>

  <asp:Repeater ID="Repeater1" runat="server" 
  onitemdatabound="Repeater1_ItemDataBound">
  <ItemTemplate>
  <tr align="right" style="height:25px;">
  <td align="center"><%# DataBinder.Eval(Container.DataItem, "ptsjtmmx_xh")%></td>
  <td id="s1" style="padding-right:5px;"><%# DataBinder.Eval(Container.DataItem, "ptsjtmmx_je")%></td>
  <td align="right"><input id="txtJE" type="text" t_value="" o_value="" style="width:200px; border:0px none white; height:25px; text-align:right;" onKeyPress="fkeyPress(this);" οnkeyup="fkeyUp(this);" /></td>
  </tr>
  </ItemTemplate>
  </asp:Repeater>

  <tr align="center" style="height:25px;">
  <td style="width:50px; line-height:120%;">合计:</td>
  <td id="Sum1" style="width:150px; line-height:120%;"></td>
  <td id="Sum2" style="width:200px; line-height:120%;"></td>
  </tr>
  </table>

------解决方案--------------------------------------------------------
js DOM进行 啊

document.getElementById("设置个tableID").rows[i].cells[j]得到各个单元格,累加就可以了
------解决方案--------------------------------------------------------
HTML code
    <script type="text/javascript">
        $(document).ready(function () {
            $("#tbid tr:last td:eq(0)").html("合计:" + ($("#tbid tr").length - 2));
            var val1 = 0;
            var val2 = 0;
            $("#tbid tr:gt(0)").not(":last").each(function () {
                val1 += parseInt($(this).find("td:eq(1)").html());
                val2 += parseInt($(this).find("td:eq(2)").html());
            })
            $("#tbid tr:last td:eq(1)").html(val1);
            $("#tbid tr:last td:eq(2)").html(val2);
        })
    </script>

    <div>
        <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse; width: 400px;" id="tbid">
            <tr align="center" style="height: 25px;">
                <th style="width: 50px; line-height: 120%;">序号</th>
                <th style="width: 150px; line-height: 120%;">样文</th>
                <th style="width: 200px; line-height: 120%;">录入列</th>
            </tr>
            <tr align="center" style="height: 25px;">
                <td style="width: 50px; line-height: 120%;">12</td>
                <td style="line-height: 120%;">1</td>
                <td style="line-height: 120%;">2</td>
            </tr>
            <tr align="center" style="height: 25px;">
                <td style="width: 50px; line-height: 120%;">12</td>
                <td style="line-height: 120%;">1</td>
                <td style="line-height: 120%;">2</td>
            </tr>
            <tr align="center" style="height: 25px;">
                <td style="width: 50px; line-height: 120%;">12</td>
                <td style="line-height: 120%;">1</td>
                <td style="line-height: 120%;">2</td>
            </tr>
            <tr align="center" style="height: 25px;">
                <td style="width: 50px; line-height: 120%;">合计:</td>
                <td id="Sum1" style="width: 150px; line-height: 120%;"></td>
                <td id="Sum2" style="width: 200px; line-height: 120%;"></td>
            </tr>
        </table>
    </div>

 

 

方式四

循环绑定的数据表累加

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值