Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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

This is because the items are still binding. The Count is going to be +1 of the current item index, when binding.

I think it would be best to do this after the repeater has bound entirely.

Therefore you could add the following to your Page_Load:

rep.DataBind()

For each item as repeateritem in rep.items
   if item.ItemIndex = (rep.Items.Count-1)
       'do for the last row
   else
       'do for all other rows
   end if
Next

Note: I've just added rep.DataBind() to show this should be ran after the repeater is bound.

This is an old question but I had this exact situation recently. I needed to write out markup for every item except the last.

I created a private member variable in my user control class and set it to the count property of the data source being bound to my repeater and subtracted 1 from it. Since an index is zero based, the index value is one off from the count.

private long itemCount { get; set; }

In Page_Load or whatever method you call DataBind:

            //Get the count of items in the data source. Subtract 1 for 0 based index.
            itemCount = contacts.Count-1;

            this.repContacts.DataSource = contacts;
            this.repContacts.DataBind();

Finally, in your binding method

           //If the item index is not = to the item count of the datasource - 1

            if (e.Item.ItemIndex != itemCount)
                Do Something....

When databinding is happening the datasource will be set for the repeater. So in your ItemDataBound event handler you can get it and check it against your item index. This would avoid the use of Session or local variables.

var datasource = (ICollection)reWorkTags.DataSource;

if (e.Item.ItemIndex + 1 == datasource.Count)
{
    // Do stuff
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top