0

我正在尝试将 July 列中的所有值汇总到 ID 为 july 的标签中。这是标记:

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

我需要做什么才能使其正常工作?

4

2 回答 2

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 事件

于 2013-11-21T23:14:41.290 回答
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是什么。

于 2013-11-21T23:14:17.587 回答