ASP.NET Repeater - 汇总总计
一种通用的方法,用于向 ASP.NET Repeater 控件添加汇总总计。
引言
这是我的第一篇文章,希望我做得正确。我不想用过多的文字来累赘,所以主要是代码,希望足够了。我想要一种简单且通用的方法,为数据 repeater 的页脚记录添加汇总总计。
背景
我的需求是为一个 datatable 绑定到 Repeater
,简单明了,为每个报表中的金额和计数列提供汇总总计。该表是从 SQL Server 通过我的后端直接获取的报表。
使用代码
我已经将所有代码都放入一个文件中,因此您可以将其复制到任何 ASP.NET 项目并直接运行。
- 首先,我们需要一个私有变量来保存需要汇总的表中字段的汇总总计。
- 接下来的代码纯粹是为了向 datatable 添加示例数据。实际报表的数据显然来自您的数据源。
#region - This code belong in your backend - // In my framework this method is a static in a DbHelper class private void addDataColumn(DataTable table, string colName, string colType, bool colNull, object colDefault) { DataColumn column = new DataColumn(); column.DataType = System.Type.GetType(colType); column.ColumnName = colName; column.AllowDBNull = colNull; column.DefaultValue = colDefault; table.Columns.Add(column); } // Here we create the sample data for this report private DataTable createDataTable() { // This is your variable that will hold the report data DataTable dtData = new DataTable(); addDataColumn(dtData, "RecordId", "System.Int32", false, 0); addDataColumn(dtData, "Description", "System.String", false, 0); addDataColumn(dtData, "TotalCount", "System.Int32", false, 0); addDataColumn(dtData, "TotalAmount", "System.Decimal", false, 0); // Fill Table with data for (int i = 1; i < 6; ++i) { DataRow dr = dtData.NewRow(); dr["RecordId"] = i; dr["Description"] = "Description for row " + i; dr["TotalCount"] = i; dr["TotalAmount"] = i * 3.34m; dtData.Rows.Add(dr); } dtData.AcceptChanges(); return dtData; } #endregion
- 接下来的方法是通用部分,它循环遍历表并汇总您在字典中添加的字段。同样,我将此方法添加到静态辅助类,因为您可以重用它。
// I have this method in a static helper class private void sumDataTable(DataTable dt, Dictionary<string, decimal> totals) { List<string> keyList = new List<string>(totals.Keys); for (int i = 0; i < dt.Rows.Count; ++i) { foreach (string key in keyList) { totals[key] += Convert.ToDecimal(dt.Rows[i][key]); } } }
- 现在我们有了专门针对报表表单的源代码,从页面加载开始。
protected void Page_Load(object sender, EventArgs e) { bindTableToForm(); }
然后在我的绑定区域,我们有
- 创建并获取示例数据。
- 将需要汇总的列添加到
Dictionary<fieldName, startvalue>
中。 - 绑定您的数据。
#region - Bind Methods - private void bindTableToForm() { // This is your variable that will hold the report data DataTable dtReport = createDataTable(); // Do Total columns totals.Add("TotalCount", 0); totals.Add("TotalAmount", 0); sumDataTable(dtReport, totals); // rprData.DataSource = dtReport; rprData.DataBind(); } #endregion
- 我们需要向页面添加受保护/公共方法,以便
Repeater
可以访问字典中的汇总总计。#region - Public methods - protected string getTotalFor(string field, string formatString) { decimal value = 0; totals.TryGetValue(field, out value); return value.ToString(formatString); } #endregion
- 最后是 ASP.NET HTML 代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RepeaterSummaryTotal.aspx.cs" Inherits="IQ.Web.RepeaterSummaryTotal" %> <!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></title> </head> <body> <form id="form1" runat="server"> <div> <div id="reportResults"> <br /> <asp:Repeater ID="rprData" runat="server"> <HeaderTemplate> <table id="tblData""> <thead> <tr> <th colspan="4"> Report sample </th> </tr> <tr> <td> Id. </td> <td> Description </td> <td style="text-align:right"> Total count </td> <td style="text-align:right"> Total amount </td> </tr> </thead> </HeaderTemplate> <ItemTemplate> <tr class="even"> <td> <%# Eval("RecordId")%> </td> <td> <%# Eval("Description")%> </td> <td style="text-align:right"> <%# Eval("TotalCount")%> </td> <td style="text-align:right"> <%#String.Format("{0:C}", Eval("TotalAmount"))%> </td> </tr> </ItemTemplate> <AlternatingItemTemplate> <tr class="odd"> <td> <%# Eval("RecordId")%> </td> <td> <%# Eval("Description")%> </td> <td style="text-align:right"> <%# Eval("TotalCount")%> </td> <td style="text-align:right"> <%#String.Format("{0:C}", Eval("TotalAmount"))%> </td> </tr> </AlternatingItemTemplate> <FooterTemplate> <tr class="subHeadingTr" style="font-weight:bold; background-color: #e8e8e8;"> <td colspan="2"> Totals </td> <td style="text-align:right"> <%# getTotalFor("TotalCount","")%> </td> <td style="text-align:right"> <%# getTotalFor("TotalAmount","C")%> </td> </tr> </table> </FooterTemplate> </asp:Repeater> </div> </div> </form> </body> </html>
#region - Private variables -
// This will be a list of all the fields that needs to be totalled - Dictionary<fieldName, totalValue>
private Dictionary<string, decimal> totals = new Dictionary<string, decimal>();
#endregion