ASP.NET Repeater Template, Conditional Code for every Nth element

后端 未结 4 847
梦毁少年i
梦毁少年i 2021-02-13 13:34

I\'m using an asp.net repeater to create a bunch of images. The image markup is all the same so the standard is fine.

However, I want

4条回答
  •  逝去的感伤
    2021-02-13 13:56

    Add two empty label controls into your Repeaters ItemTemplate where you'd want your div tags to be.

    Then add an ItemDataBound event to the Repeater.

    Then add this code into the ItemDataBound event:

        Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
            If e.Item.ItemIndex Mod 5 = 0 Then
                Dim lblDivStart As Label = CType(e.Item.FindControl("lblDivStart"), Label)
                Dim lblDivEnd As Label = CType(e.Item.FindControl("lblDivEnd"), Label)
                lblDivStart.text = "
    " lblDivEnd.text = "
    " End If End If End Sub

    Note - This will need some tweaking to handle the first div and you may need to do something like If (e.Item.ItemIndex + 1) Mod 5 = 0 to get the divs to show up exactly where you want them.

    For more info:
    DataListItem.ItemIndex Property
    DataList.ItemDataBound Event

提交回复
热议问题