首页 新闻 搜索 专区 学院

asp.net DATAlist添加 合计栏

0
[已解决问题] 解决于 2011-12-21 09:12
 1 <asp:DataList ID="DataList1" runat="server" Width="100%" BorderWidth="0px" CellPadding="0">
2 <ItemTemplate>
3 <table style="width: 100%;" border="1" cellspacing="0" cellpadding="0">
4 <tr>
5 <td style="width: 51px;">
6 <asp:Label ID="Label1" runat="server" Text='<%#Eval("mgname")%>'></asp:Label>
7 </td>
8 <td style="width: 51px;">
9 <asp:Label ID="Label8" runat="server" Text='<%#Eval("goalcount")%>'></asp:Label>
10 </td>
11 <td style="width: 51px;">
12 <asp:Label ID="Label9" runat="server" Text='<%#Eval("truecount")%>'></asp:Label>
13 </td>
14 <td style="width: 51px;">
15 <asp:Label ID="Label2" runat="server" Text='<%#Eval("finshrate")%>'></asp:Label>
16 </td>
17 </tr>
18 </table>
19 </ItemTemplate>
20 <FooterTemplate>
21 <table style="width: 100%;" border="1" cellspacing="0" cellpadding="0">
22 <tr>
23 <td style="width: 51px;">
24 <asp:Label ID="Label" runat="server" Text='总计'></asp:Label>
25 </td>
26 <td style="width: 51px;">
27 <asp:Label ID="Label3" runat="server" Text=''></asp:Label>
28 </td>
29 <td style="width: 51px;">
30 <asp:Label ID="Label6" runat="server" Text=''></asp:Label>
31 </td>
32 <td style="width: 51px;">
33 <asp:Label ID="Label7" runat="server" Text=''></asp:Label>
34 </td>
35 </tr>
36 </table>
37 </FooterTemplate>
38 </asp:DataList>

我想在查询结果的最后一行添加一个合计,计算每一列的总和,footerTemplate那里应该怎么写啊?新手求高手指导,说的越详细越好。谢谢了!!

Mr.ch的主页 Mr.ch | 初学一级 | 园豆:6
提问于:2011-12-20 18:05
< >
分享
最佳答案
0

写了个简单的例子,你对应更改一下就OK了:

前台:

<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" Width="100%" BorderWidth="0px" CellPadding="0">
<ItemTemplate>
<table style="width: 100%;" border="1" cellspacing="0" cellpadding="0">
<tr>
<td style="width: 51px;">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("mgname")%>'></asp:Label>
</td>
<td style="width: 51px;">
<asp:Label ID="Label8" runat="server" Text='<%#Eval("goalcount")%>'></asp:Label>
</td>
<td style="width: 51px;">
<asp:Label ID="Label9" runat="server" Text='<%#Eval("truecount")%>'></asp:Label>
</td>
<td style="width: 51px;">
<asp:Label ID="Label2" runat="server" Text='<%#Eval("finshrate")%>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<table style="width: 100%;" border="1" cellspacing="0" cellpadding="0">
<tr>
<td style="width: 51px;">
<asp:Label ID="Label" runat="server" Text='总计'></asp:Label>
</td>
<td style="width: 51px;">
<asp:Label ID="Label3" runat="server" Text='<%# GetTotal("Label8") %>'></asp:Label>
</td>
<td style="width: 51px;">
<asp:Label ID="Label6" runat="server" Text='<%# GetTotal("Label9") %>'></asp:Label>
</td>
<td style="width: 51px;">
<asp:Label ID="Label7" runat="server" Text='<%# GetTotal("Label2") %>'></asp:Label>
</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</div>
</form>
</body>

后台代码:

public partial class DataListCount : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var testList = new List<Test>()
{
new Test(){ mgname="a",goalcount=1,truecount=2,finshrate=3},
new Test(){ mgname="a",goalcount=1,truecount=2,finshrate=3},
new Test(){ mgname="a",goalcount=1,truecount=2,finshrate=3},
new Test(){ mgname="a",goalcount=1,truecount=2,finshrate=3},
new Test(){ mgname="a",goalcount=1,truecount=2,finshrate=3},
new Test(){ mgname="a",goalcount=1,truecount=2,finshrate=3},
new Test(){ mgname="a",goalcount=1,truecount=2,finshrate=3},
new Test(){ mgname="a",goalcount=1,truecount=2,finshrate=3},
new Test(){ mgname="a",goalcount=1,truecount=2,finshrate=3}
};
DataList1.DataSource = testList;
DataList1.DataBind();
}

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();
}
}

public class Test
{
public string mgname { get; set; }
public int goalcount { get; set; }
public int truecount { get; set; }
public int finshrate { get; set; }
}

效果:

只要GetTotal("Label9")这里传的参数对应上面要计算的列的label的ID就OK了

artwl | 专家六级 |园豆:16526 | 2011-12-20 18:25

万分感谢!功能已经实现了,但是我那些数据里面有些是小数的,这个我还得琢磨琢磨

Mr.ch | 园豆:6 (初学一级) | 2011-12-21 09:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册