获得总在ListView中列 [英] Getting total for a column in ListView

查看:71
本文介绍了获得总在ListView中列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到一个总和一个ListView中列中的所有项目。我把在ItemDataBound事件以下code,但测试它,它只会越来越什么约束,哎呀。

在实现

所以,我一直在寻找一点帮助转换这展会共有来自绑定到ListView中的所有项目我的专栏。

感谢。

 如果(e.Item.ItemType == ListViewItemType.DataItem)
       {
           ListViewDataItem项目=(ListViewDataItem)e.Item;           标签lblQty =(标签)e.Item.FindControl(lblQuantity);
           如果(lblQty == NULL)
           {
               返回;
           }           如果(lblQty.Text.Length == 0 || lblQty.Text ==)
           {
               返回;
           }
           其他
           {
               ListViewTotal + = int.Parse(lblQty.Text);
           }
       }


解决方案

我发现要做到这一点,最好的方法是实施你是控制在 OnDataBinding 方法捆绑。例如:

 < ASP:ListView控件ID =ListView1的=服务器>
    <&ItemTemplate中GT;
        < ASP:文字ID =yourLiteral=服务器
            OnDataBinding =yourLiteral_DataBinding>< / ASP:文字>
    < / ItemTemplate中>
< / ASP:的ListView>

首先在你的.cs定义一个全局计数器:

 私人INT _quantityTotal = 0;

然后实施 OnDataBinding 的控制:

 保护无效yourLiteral_DataBinding(对象发件人,发送System.EventArgs)
 {
     //你可以得到任何东西在这里得到包括调用一个DB值
     //调用,如果你想为被绑定的每个项目。
     文字LT =(文字)(寄件人);
     INT量=(INT)(EVAL(yourQuantityField));
     _quantityTotal + =数量;
     lt.Text = quantity.ToString();
 }

现在已存储在 _quantityTotal 总,然后可以添加到页脚或东西绑定后其他人发生像 OnDataBound 事件。

I need to get a sum for all items in a column within a listview. I put in the following code in the itemdatabound event, but realized after testing it that it will only be getting what is bound, oops.

So I was looking for a little help converting this to show a total for my column from all items bound to the ListView.

Thanks.

 if (e.Item.ItemType == ListViewItemType.DataItem)     
       {
           ListViewDataItem item = (ListViewDataItem)e.Item;         

           Label lblQty = (Label)e.Item.FindControl("lblQuantity");
           if (lblQty == null)
           {
               return;
           }

           if (lblQty.Text.Length == 0 || lblQty.Text == "")
           {
               return;
           }
           else
           {
               ListViewTotal += int.Parse(lblQty.Text);
           }
       }

解决方案

The best method I have found to do this is to implement the OnDataBinding method for the control you are binding. For example:

<asp:ListView ID="ListView1" runat="server">
    <ItemTemplate>
        <asp:Literal ID="yourLiteral" runat="server"
            OnDataBinding="yourLiteral_DataBinding"></asp:Literal>
    </ItemTemplate>
</asp:ListView>

First define a global counter in your .cs:

private int _quantityTotal = 0;

Then implement the OnDataBinding for the control:

 protected void yourLiteral_DataBinding(object sender, System.EventArgs e)
 {
     // You could get anything here to get a value including calling a DB
     // call if you want for each item that is being bound.
     Literal lt = (Literal)(sender);
     int quantity = (int)(Eval("yourQuantityField"));
     _quantityTotal += quantity;
     lt.Text = quantity.ToString();
 }

Now you have the total stored in _quantityTotal which you can then add to a footer or something else after the databinding has occurred like the OnDataBound event.

这篇关于获得总在ListView中列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆