asp.netrepeaterasp.net-4.0itemdatabound

Get row count in ItemDataBound


I have a datasource in Page_Load binded to a repeater.

I am writing the results to the page in ItemDataBound but when it's the last row of data I require it to do something slightly different.

How do I access the row count of a data source in Page_Load from within ItemDataBound of the repeater?

I've tried:

Dim iCount As Integer
iCount = (reWorkTags.Items.Count - 1)
If e.Item.ItemIndex = iCount Then
    'do for the last row
Else
    'do for all other rows
End If

But both e.Item.ItemIndex and iCount equal the same for every row.

Thanks for any help. J.


Solution

  • Was tring to avoid using Sessions but got it working with one in the end.

    I just created a session of the row count and could access that from ItemDataBound.

    Protected Sub reWorkTags_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles reWorkTags.ItemDataBound
    
    
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
    
            Dim rowView As System.Data.DataRowView
            rowView = CType(e.Item.DataItem, System.Data.DataRowView)
    
            Dim link As New HyperLink
            link.Text = rowView("tag")
            link.NavigateUrl = rowView("tagLink")
            link.ToolTip = "View more " & rowView("tag") & " work samples"
    
            Dim comma As New LiteralControl
            comma.Text = ", "
    
            Dim workTags1 As PlaceHolder = CType(e.Item.FindControl("Linkholder"), PlaceHolder)
    
            If e.Item.ItemIndex = Session("iCount") Then
                workTags1.Controls.Add(link)
            Else
                workTags1.Controls.Add(link)
                workTags1.Controls.Add(comma)
            End If
    
        End If
    
    End Sub