在ItemDataBound中退出Sub以获取转发器

时间:2013-03-01 15:10:56

标签: c# asp.net .net vb.net

我希望在第4次迭代时退出rpt.ItemDataBound函数,但是当我完成时:

 Protected Sub rptCol_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rptCol.ItemDataBound
        If Not e.Item.ItemType = ListItemType.AlternatingItem AndAlso Not e.Item.ItemType = ListItemType.Item Then Exit Sub
        If e.Item.ItemIndex = 4 Then
            Exit Sub
        End If

...

它不起作用,他只是跳过这个迭代。

任何想法? 感谢

4 个答案:

答案 0 :(得分:2)

就像@Marcus所说的那样,迭代会继续,因为每一行都会调用它。

尝试不同的方法。在绑定到转发器之前更改数据源。像这样:

//I am assuming your datasource is a List, but this works for a datatable, etc
List<[YOUR CLASS]> datasource = MethodThatGetsYourSource();
rptCol.DataSource = datasource.Take(4);
rptCol.DataBind();

答案 1 :(得分:1)

Private Sub ForceStopAfterFirstBind(sender As Object, e As RepeaterItemEventArgs)
    If e.Item.ItemIndex > 3 Then
        e.Item.Controls.Clear()
    End If
End Sub

这样称呼:

ForceStopAfterFirstBind(sender, e)

答案 2 :(得分:0)

我认为你需要尝试itemCreadedOnDatabound事件

答案 3 :(得分:0)

迭代将继续,因为将继续为每一行调用事件处理程序。如果想在某一行之后跳过逻辑,你可以这样做:

Protected Sub rptCol_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rptCol.ItemDataBound
    If Not e.Item.ItemType = ListItemType.AlternatingItem AndAlso Not e.Item.ItemType = ListItemType.Item Then Exit Sub
    If e.Item.ItemIndex > 3 Then
        Exit Sub
    End If
 .....
End Sub