我正在尝试将7月列中的所有值汇总到ID为7月的标签中。这是标记:
<asp:ListView ID="leaveListView" runat="server" ItemPlaceholderID="itemPlaceholder" OnItemDataBound="leaveListView_ItemDataBound">
<LayoutTemplate>
<table class="budgetList">
<thead>
<tr>
<td>Project Number</td>
<td>Account</td>
<td>Project Name</td>
<td>July</td>
</tr>
</thead>
<tbody>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</tbody>
<tfoot>
<tr class="projectRowTotal">
<td>Project Totals</td>
<td></td>
<td></td>
<td><asp:Label ID="july" runat="server" Text=""></asp:Label></td>
</tr>
</tfoot>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class="<%# Container.DataItemIndex % 2 == 0 ? "" : "even" %>">
<td><%# Eval("Project") %></td>
<td><%# Eval("Account") %></td>
<td><%# Eval("ProjectNumber") %></td>
<td><%# Eval("July") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
我正在使用ItemDataBound事件求和,但它返回null。这是代码隐藏的代码:
decimal july = 0m;
protected void leaveListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
july += Convert.ToDecimal(e.Item.FindControl("July"));
}
Label julySum = (Label)leaveListView.FindControl("july");
julySum.Text = string.Format("{0:C}", (decimal?)july);
}
我需要做些什么才能让它发挥作用?
答案 0 :(得分:2)
正如我的意见所示,这是解决方案:
<table class="budgetList">
<thead>
<tr>
<td>Project Number</td>
<td>Account</td>
<td>Project Name</td>
<td>July</td>
</tr>
</thead>
<tbody>
<asp:ListView ID="leaveListView" runat="server" ItemPlaceholderID="itemPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<tr class="<%# Container.DataItemIndex % 2 == 0 ? "" : "even" %>">
<td><%# Eval("Project") %></td>
<td><%# Eval("Account") %></td>
<td><%# Eval("ProjectNumber") %></td>
<td><%# Eval("July") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
</tbody>
<tfoot>
<tr class="projectRowTotal">
<td>Project Totals</td>
<td></td>
<td></td>
<td><asp:Label ID="july" runat="server" Text=""></asp:Label></td>
</tr>
</tfoot>
</table>
还没有真正尝试过,但应该可以正常工作...... +在代码中绑定数据:
leaveListView.ItemsSource = <data>;
// simply place the value in label directly
july.Text = <data.sum>
现在只需在代码中进行总结即可完成,无需使用itemdatabound事件
答案 1 :(得分:0)
e.Item.FindControl("July")
返回null,因为无法控制ListView
您希望将July
的值存储到HiddenField(或其他某个字段)中,以便您可以在ItemDataBound事件中检索它们。
<asp:ListView ID="leaveListView"...>
<ItemTemplate>
<tr class="<%# Container.DataItemIndex % 2 == 0 ? "" : "even" %>">
<td><%# Eval("Project") %></td>
<td><%# Eval("Account") %></td>
<td><%# Eval("ProjectNumber") %></td>
<td><%# Eval("July") %></td>
</tr>
<asp:HiddenField runat="server" ID="JulyHiddenField"
Value="<%# Eval("July") %>"/>
</ItemTemplate>
</asp:ListView>
decimal july = 0m;
protected void leaveListView_ItemDataBound(object sender,
ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var hiddenField = e.Item.FindControl("JulyHiddenField") as HiddenField;
july += Convert.ToDecimal(hiddenField.Value);
}
Label julySum = (Label)leaveListView.FindControl("july");
julySum.Text = string.Format("{0:C}", (decimal?)july);
}
请正确命名您的控件。例如。 <asp:Label ID="JulyLabel" runat="server" Text=""></asp:Label>
。目前,您无法知道july
是什么。