views:

58

answers:

2

I want to evaluate the current DataItem being bound to a repeater and remove it from being added if my condition meets. I would have thought that setting e.Item.DataItem to Nothing would work, but it does not. Any ideas how to not add a DataItem to the repeater when a certain condition meets?

Protected Sub rpt_OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        If true Then
            //don't add the current item
            e.Item.DataItem = Nothing
        Else
            //add the current item
        End If
    End If
End Sub
+1  A: 

You're going about it the wrong way. At this point in the code, an item has already been added, and you're just binding (populating) it. What you want to do is filter your result set before you set it as the DataSource, before your call to repeater.DataBind

David Hedlund
Unfortunately, that will not work and was what I really wanted to do to begin. Hard to explain the situation, I guess. But knowing that it's too late to do this sort of thing in ItemDataBound is enough I guess.
mccrager
well in that unfortunate situation, if i had the chance, i'd wrap the entire `ItemTemplate` in a panel, and when the condition of interest is met, i'd set the panels visibility to false.
David Hedlund
of course that approach would mess things up if you're using `AlternatingItemTemplate`, for, say, a different background color. to work around that, you'd have to have an external counter of the number of items actually visible so far, and set the background based on `visibleItems%2==0`, but now it's really starting to get dirty. i hope this is not your situation
David Hedlund
+1  A: 

Have you tried setting the Item's visibility to False?

If true Then
  //don't add the current item
  e.Item.Visible = False
Else
  //add the current item
End If
Shawn Steward