统计DataList某列值的总和

Personnel.cs:
/// <summary>
/// 职员类
/// </summary>
public class Personnel
{
    private String _Name;
    private Int32 _Wage;


    public Personnel(String name, Int32 wage)
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //

        _Name = name;
        _Wage = wage;
    }

    /// <summary>
    /// 姓名
    /// </summary>
    public String Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    /// <summary>
    /// 工资
    /// </summary>
    public Int32 Wage
    {
        get { return _Wage; }
        set { _Wage = value; }
    }
}

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>职员工资表-Mzwu.Com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("wage") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="Label3" runat="server" Text='总计'></asp:Label>
                <asp:Label ID="Label4" runat="server" Text='<%# GetTotal("Label2") %>'></asp:Label>
            </FooterTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>

Default.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Personnel> classa = new List<Personnel>();

        Personnel aa = new Personnel("张三", 1000);
        Personnel bb = new Personnel("李四", 1500);
        Personnel cc = new Personnel("王五", 1500);
        Personnel dd = new Personnel("陈七", 2000);

        classa.Add(aa);
        classa.Add(bb);
        classa.Add(cc);
        classa.Add(dd);

        DataList1.DataSource = classa;
        DataList1.DataBind();

    }

    /// <summary>
    /// 统计某列的总值
    /// </summary>
    /// <param name="objName">Label控件名称</param>
    /// <returns>总值</returns>
    protected String GetTotal(String objName)
    {
        Label myLabel;
        Int32 returnValue = 0;

        for(Int32 i=0;i<DataList1.Items.Count;i++)
        {
            myLabel = (Label)DataList1.Items[i].FindControl(objName);
            returnValue += Convert.ToInt32(myLabel.Text);
        }

        return returnValue.ToString();
    }
}

需要注意的是统计总和必须放在FooterTemplate模板中进行,如果在HeaderTemplate模板中进行统计,则结果将为0!

评论: 0 | 引用: 0 | 查看次数: 4628
发表评论
登录后再发表评论!