Active Count of Rows using Repeater Control (ASP.NET VB)

R

RC-

Hi everyone,
I have been searching and searching for an answer to this question using
Google and what not; I have not been able to find a "clear cut" answer.

OK, now the question:

I have a Repeater control that works great. What I am trying to do is
perform an active count of the number of rows that the repeater is
rendering. The end goal is to insert a page break after six rows of data. I
cannot figure out how to count the rows one by one using ASP.NET (ASP is
easy).

Any help will be greatly appreciated.

TIA,
RC-
 
R

Ray Costanzo

Perhaps e.Item.ItemIndex (+1)

Example:


--ASPX:
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
something<br />
<asp:Literal ID="lit" runat="server" Visible="false"><div>Put your page
break div here.</div></asp:Literal>
</ItemTemplate>
</asp:Repeater>


--VB:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
''Just some bogus data for demo purposes:
Dim dt As New DataTable()
dt.Columns.Add("ID", Type.GetType("System.Int32"))
dt.Columns.Add("Text", Type.GetType("System.String"))
Dim q As Integer
For q = 1 To 100
Dim r As DataRow = dt.NewRow()
r("ID") = q
r("Text") = "Item #" & CStr(q)
dt.Rows.Add(r)
Next
rpt.DataSource = dt
rpt.DataBind()
End Sub

Protected Sub rpt_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rpt.ItemDataBound
If (e.Item.ItemIndex + 1) Mod 6 = 0 Then
CType(e.Item.FindControl("lit"), Literal).Visible = True
End If
End Sub
 
R

RC-

Ray,
Thank you for the post; I've almost got it to work, not entirely sure what
the code does though (very new to .NET).

I changed a couple of very small things on your original post; I removed the
bogus data and changed the name of the repeater to match my own.

I am still getting an error:

NullReferenceException was unhandled by user code

What the heck does that mean and what can I do? ( :)

The code seems to run until the end then it throws the above error.


Thanks again,
RC-
 
R

RC-

OK, I figured out why I was getting the NullReferenceException message; I
have a HeaderTemplate, SeperatorTemplate, and FooterTemplate element. I made
the code only fire on the Item element. But now the other code is not
triggering.

Here's the code I've modified:

Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs) _
Handles Repeater1.ItemDataBound
If (e.Item.ItemType = ListItemType.Item) Then

If (e.Item.ItemIndex + 1) Mod 6 = 0 Then

CType(e.Item.FindControl("lit"), Literal).Visible = True

End If

End If

End Sub


Any idea why my code is not working now?
 
R

Ray Costanzo

Hi RC-,

I can't explain this, but in testing, I got the same results as you. I
discovered that if I change the IF condition, it works as expected. I'm not
sure why this is, however. Try this modified version:


Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs) _
Handles Repeater1.ItemDataBound
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <>
ListItemType.Footer And e.Item.ItemType <> ListItemType.Separator Then
If (e.Item.ItemIndex + 1) Mod 6 = 0 Then
CType(e.Item.FindControl("lit"), Literal).Visible = True
End If
End If
End Sub


I wrote out the itemindex for e as I looped through it with the other code,
and it didn't have an index on any odd number items. Then, in another
repeateron the page, the index only existed 6 items.

Ray at work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top