DataItem in last row of Repeater data not being formatted

J

John Kotuby

Hi all,

Thanks in advance for any suggestions...

I am using a repeater to display the contents of a DataTable which is bound
to it. All the rows in the table are being displayed. In the OnItemDataBound
event controller in the Code Behind I am formatting the fields returned from
the SQL server select query. The items are formatting correctly, except for
those in the last row of the bound table. To test my theory, I added a row
to the result set, with data that is nearly identical to the row that was
not being formatted. As I expected, the next-to-last row is now being
formatted properly, but the newly added row is not.

Here is the code I am using. Can anyone tell me why the last row of data is
being ignored?

I am defining the format of just the ItemTemplate in the Repeater
declaration HTML.

Note, I am iterating through the RepeaterItemCollection because that is a
code example I saw in a newsgroup and it is the only way I could get at the
particular label (so far). I am guessing there must be a simpler way to get
to a particular label control without searching the entire collection in
the repeater, but I haven't found that code example yet.

--------------------------------
Sub RepSeg_DataBound(ByVal Sender As Object, ByVal e As
RepeaterItemEventArgs)
' This event is raised for the header, the footer, separators, and items.
Dim lblCostFld As Label
Dim rc As RepeaterItemCollection = RepeaterSeg.Items
' Execute the following logic for Items and Alternating Items.

If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then

For Each Item As RepeaterItem In rc
lblCostFld = CType(Item.FindControl("lblCost"), Label)
If Not lblCostFld Is Nothing Then
segCost = RTrim(lblCostFld.Text)
segCost = Replace(segCost, "/", "")
segCost = Replace(segCost, "M", "")
segCost = FormatCurrency(segCost)
lblCostFld.Text = segCost & "/M"
Else
'*** JPK Debug ***
Response.Write("lblCost field not found")
Response.End()
'*** JPK Debug ***
End If
Next

End If

End Sub
 
E

Eliyahu Goldin

Get rid of the For Each loop, it doesn't make any sense here. ItemDataBound
event is fired for every item, you don't need to go through all previous
items over and over again.Get to the label as e.Item.FindControl("lblCost").


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
J

John Kotuby

Thank you Eliyahu,

Your suggestion fixed the problem and streamlined the code. I appreciate the
syntax lesson....

Eliyahu Goldin said:
Get rid of the For Each loop, it doesn't make any sense here.
ItemDataBound event is fired for every item, you don't need to go through
all previous items over and over again.Get to the label as
e.Item.FindControl("lblCost").


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


John Kotuby said:
Hi all,

Thanks in advance for any suggestions...

I am using a repeater to display the contents of a DataTable which is
bound to it. All the rows in the table are being displayed. In the
OnItemDataBound event controller in the Code Behind I am formatting the
fields returned from the SQL server select query. The items are
formatting correctly, except for those in the last row of the bound
table. To test my theory, I added a row to the result set, with data that
is nearly identical to the row that was not being formatted. As I
expected, the next-to-last row is now being formatted properly, but the
newly added row is not.

Here is the code I am using. Can anyone tell me why the last row of data
is being ignored?

I am defining the format of just the ItemTemplate in the Repeater
declaration HTML.

Note, I am iterating through the RepeaterItemCollection because that is a
code example I saw in a newsgroup and it is the only way I could get at
the particular label (so far). I am guessing there must be a simpler way
to get to a particular label control without searching the entire
collection in the repeater, but I haven't found that code example yet.

--------------------------------
Sub RepSeg_DataBound(ByVal Sender As Object, ByVal e As
RepeaterItemEventArgs)
' This event is raised for the header, the footer, separators, and items.
Dim lblCostFld As Label
Dim rc As RepeaterItemCollection = RepeaterSeg.Items
' Execute the following logic for Items and Alternating Items.

If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then

For Each Item As RepeaterItem In rc
lblCostFld = CType(Item.FindControl("lblCost"), Label)
If Not lblCostFld Is Nothing Then
segCost = RTrim(lblCostFld.Text)
segCost = Replace(segCost, "/", "")
segCost = Replace(segCost, "M", "")
segCost = FormatCurrency(segCost)
lblCostFld.Text = segCost & "/M"
Else
'*** JPK Debug ***
Response.Write("lblCost field not found")
Response.End()
'*** JPK Debug ***
End If
Next

End If

End Sub
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top