Insertion of a page break in a repeater

I was in a situation where I need to break up the data into a separate page after a certain amount of records (in my case 3). I chose to use repeater as I have a full control of the HTML layout and how I want my form to appear in a print format. I posted my question in ASP.NET forums and to my delight received a working code after a full day of torture and doing search in Google. I thought I would share here the snippet that can be useful when a specific scenario arise. It is not a lot of code but it saved me another day of frustration. Now it's time to put it on the side and move on with new projects.

The code below can be altered by changing the page size. Take note that the sub procedure below uses ItemDataBound event, which occurs after the binding of data and before it's displayed on the page (print in this case). 

        Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
            Dim pageSize As Integer = 3
            If (e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem) AndAlso ((e.Item.ItemIndex + 1) Mod pageSize = 0) Then
                e.Item.Controls.Add(New LiteralControl("<p style='PAGE-BREAK-AFTER: always'>&nbsp;</p>"))
            End If
        End Sub


  • nice! this has been really helpful

    my C# version
    protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
    if ((e.Item.ItemType.Equals(ListItemType.Item) || e.Item.ItemType.Equals(ListItemType.AlternatingItem)) && (e.Item.ItemIndex + 1) % ProductsPerPage == 0)
    e.Item.Controls.Add(new LiteralControl(""));

  • i have the next error:

    The Controls collection cannot be modified because the control contains code blocks

Comments have been disabled for this content.