asp.netvb.netrepeateritemdatabound

total in repeater footer, itemdatabound in VB.NET



I want to sum the total of the gender at the repeater footer using vb.net by using item data bound, the behind code is wrong cause I don't know how to do it...

FRONT CODE

<asp:Repeater ID="repGender" runat="server">
                <HeaderTemplate>
                    <table cellspacing="0" rules="all" border="1">
                        <tr>
                            <th>Gender</th>
                            <th>Total</th>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <%# Eval("GENDER")%>
                        </td>
                        <td style="text-align: center">
                           <%# Eval("TOTAL")%></a>
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    <tr style="font-weight: bold">
                        <td>Grand Total</td>
                        <td style="text-align: center">
                            <asp:Label runat="server" ID="lblTotal"></asp:Label>
                        </td>
                    </tr>
                    </table>
                </FooterTemplate>
            </asp:Repeater>

BEHIND CODE

Protected Sub repGender_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repGender.ItemDataBound
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Total += Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, ""))
        Else e.Item.ItemType == ListItemType.Footer
            


        End If
    End Sub

Solution

  • Ok, so we need to define a simple total var at the form's "class" level. it only needs to persist during the data bind operation.

    So, given your above markup, then we have this code to load the repeater.

    Dim MyTotal As Integer
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
            LoadData
        End If
    
    End Sub
    
    Sub LoadData()
    
        Dim strSQL As String =
            "SELECT Gender, count(*) as TOTAL
            FROM Employee
            GROUP BY Gender"
    
        Dim rstData As New DataTable
    
        Using mycon As New SqlConnection(GetConstr)
            Using cmdSQL As New SqlCommand(strSQL, mycon)
                mycon.Open()
                rstData.Load(cmdSQL.ExecuteReader)
            End Using
        End Using
    
        MyTotal = 0
        repGender.DataSource = rstData
        repGender.DataBind()
    
    End Sub
    

    And with your markup we see/get this:

    enter image description here

    And our data row bind event looks like this:

    Protected Sub repGender_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repGender.ItemDataBound
    
        If e.Item.ItemType = ListItemType.Item Or
            e.Item.ItemType = ListItemType.AlternatingItem Then
    
            ' We don't have lable or text box with an "id" in repeat
            ' section, (can't use find control unless you assign "id" to these controls).
            ' so, lets grab the whole data row used for binding
    
            Dim gData As DataRowView = e.Item.DataItem
    
            MyTotal = MyTotal + gData.Item("TOTAL")
    
        End If
    
        If e.Item.ItemType = ListItemType.Footer Then
    
            ' set the total value in footer
            Dim lblTotal As Label = e.Item.FindControl("lblTotal")
            lblTotal.Text = MyTotal
    
        End If
    
    End Sub