Question

I'm trying to sum all the values from the July column into a label with ID of july. Here's the markup:

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

I'm using the ItemDataBound event to sum, but it's returning null. This is the code-behind:

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

What do I need to do to get this to work?

Was it helpful?

Solution

as my comments suggested, here is solution:

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

havent't actually tried it, but should work fine ... + in code, where you bind data:

 leaveListView.ItemsSource = <data>;
 // simply place the value in label directly
 july.Text = <data.sum>

now just make sum in code and you're done, no need for itemdatabound event

OTHER TIPS

e.Item.FindControl("July") return null because no control with that existing in the ListView

Solution

You want to store July's value into HiddenField (or some other field) so that you can retrieve them back in ItemDataBound event.

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

For Good Design Practice

Please name your control properly. Eg. <asp:Label ID="JulyLabel" runat="server" Text=""></asp:Label>. Currently, you have no way of knowing is july is what.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top