By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 2,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

DataItem in last row of Repeater data not being formatted

P: n/a
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
Apr 18 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <jo***@powerlist.comwrote in message
news:Oq*************@TK2MSFTNGP05.phx.gbl...
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

Apr 18 '07 #2

P: n/a
Thank you Eliyahu,

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

"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote in
message news:ee**************@TK2MSFTNGP02.phx.gbl...
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" <jo***@powerlist.comwrote in message
news:Oq*************@TK2MSFTNGP05.phx.gbl...
>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


Apr 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.