Diff between DataGrid and Repeater's ItemDataBound event

This line works in my event handling of a datagrid's values but tosses an error with encounted in a repeater.
e.Item.Cells(0).BackColor = Color.PapayaWhip

the repeater's error:
'Cells' is not a member of 'System.Web.UI.WebControls.RepeaterItem'.

juststeveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ihenryCommented:
Hello Steve,

It is not like DataGridItem which inherits from TableRow and has Cells property, RepeaterItem is merely a control and not even a WebControl. Therefore it lacks many fancy properties like BackColor, ForeColor, etc.

What are you trying to achieve exactly?
0
mmarinovCommented:
Hi juststeve,

because the repeater control is created for giving the developer the freedom to design and output the html - there are no cells in the repeater control
all the work is accessible within the ItemDataBOund event with FindControl method
check this :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsrepeaterclassitemdataboundtopic.asp

Regards!
B..M
mmarinov
0
juststeveAuthor Commented:
Thx...what I'm trying to do is change the fore & back color (and even text) depending on the value contained in a given <td> (code below) - I have this working using a Datagrid but need to customize the contends of the header row so am trying to implement this in a repeater.

So it seems I need to take control of the HTML output itself ... instead of relying on .Net's cells to produce the correct code for the <td> elements I need to code them myself?

The referenced sample doesn't show manipulation at the HTML level...any samples around that explain how to approach this?

        Select Case e.Item.ItemType
            Case ListItemType.Item
                'e.Item.Cells(0).BackColor = Color.PapayaWhip
                e.Item.
                For i As Int16 = 1 To 22
                    Dim strItem As String = e.Item.Cells(i).Text
                    Dim iValue As Integer = Convert.ToInt32(strItem)
                    If iValue < 80 Then '
                        e.Item.Cells(i).BackColor = Color.Turquoise
                        'will change color of particular cell
                    End If
                    If iValue < 0 Then '
                        e.Item.Cells(i).BackColor() = Color.Crimson
                        'blank out the text (would all be '-1')
                        e.Item.Cells(i).Text = ""
                    End If
                Next

           End


0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

ihenryCommented:
You can try like this,

Change all <td>s under <ItemTemplate> to server control

<ItemTemplate>
      <tr>
            <td runat="server"><%# DataBinder.Eval(Container.DataItem, "field1") %>"</td>
            <td runat="server"><%# DataBinder.Eval(Container.DataItem, "field2") %>"</td>
            <td runat="server"><%# DataBinder.Eval(Container.DataItem, "field3") %>"</td>
      </tr>
</ItemTemplate>

And in ItemBound event,

Private 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
          For i As Integer = 0 To e.Item.Controls.Count - 1
            If e.Item.Controls(i).GetType() Is GetType(HtmlTableCell) Then
                Dim c As HtmlTableCell = CType(e.Item.Controls(i), HtmlTableCell)
                Dim strItem As String = c.InnerText
                Dim iValue As Integer = Int32.Parse( strItem )
                If iValue < 80 Then '
                  ....
                  ....
                  ....
                End If
            End If
          Next
      End If
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ihenryCommented:
Aah..I forgot something, my mistake..

Dim c As HtmlTableCell = CType(e.Item.Controls(i), HtmlTableCell)
' set background color
c.Attributes.Add( "bgcolor", "red" )
c.Attributes.Add( "text", "white" )
0
juststeveAuthor Commented:
Thankx...I think we're real close here... I'm getting a page exception - System.FormatException: . - on the line setting iValue (comment in code below). Note that I'm only manipulating columns starting at '1' - i'm skipping column 0 cuz that has alph characters in it. All other columns have an interger above and including '-1'.

This is probably where an exerienced VS.net programmer steps into the debugger but alas, I'm not - so i'm at a loss as to how to track the error condition down.

One other quick question...in the condition where the value is '-1' I want to produce a blank table cell - is this the correct way?
    c.InnerText = ""



For i As Int16 = 1 To 22
    If e.Item.Controls(i).GetType() Is GetType(HtmlTableCell) Then
      Dim c As HtmlTableCell = CType(e.Item.Controls(i), HtmlTableCell)
      Dim strItem As String = c.InnerText
      Dim iValue As Integer = Convert.ToInt32(strItem)  
                'THE LINE ABOVE PRODUCES PAGE EXCEPTION-Input string was not in a correct format
      If iValue < 80 Then '
          c.Attributes.Add("bgcolor", "Turquoise")
          'will change color of particular cell
      End If
      If iValue < 0 Then '
          c.InnerText = ""
          c.Attributes.Add("bgcolor", "Crimson")
      End If
    End If
Next



0
ihenryCommented:
I think my previous post not good enough, try this

    For i As Integer = 0 To e.Item.Controls.Count - 1
      If e.Item.Controls(i).GetType() Is GetType(HtmlTableCell) Then
          Dim c As HtmlTableCell = CType(e.Item.Controls(i), HtmlTableCell)
          Dim strItem As String = c.InnerText.Trim()
          c.BgColor = Color.PapayaWhip.Name  ' background color
          c.InnerHtml = String.Format("<font color='{0}'>{1}</font>", Color.Magenta.Name, t) ' text color
      End If
    Next

And the error means, the Convert.ToInt32(strItem)  failed to return an integer value. Are you sure all <td> tags under <ItemTemplate> contain just numeric value? or maybe there's spaces inside a <td>.
0
ihenryCommented:
>> One other quick question...in the condition where the value is '-1'
>> I want to produce a blank table cell - is this the correct way?
>>    c.InnerText = ""
Yes

And you seem need to try debugging, since I'm not very sure how your table structure looks like.
0
mmarinovCommented:
Hi juststeve,

i see ihenry has suggested already a good solution to the problem, but i'm curious what was te problem to customize the header when you use datagrid ?

B..M
mmarinov
0
ihenryCommented:
If you feel difficult to do debugging, try the following code. You'll know which cell is giving problem

            For i As Integer = 0 To e.Item.Controls.Count - 1
                If e.Item.Controls(i).GetType() Is GetType(HtmlTableCell) Then
                    Dim c As HtmlTableCell = CType(e.Item.Controls(i), HtmlTableCell)
                    Dim strItem As String = c.InnerText.Trim()
                    Try
                        Dim iValue As Integer = Convert.ToInt32(strItem)
                        If iValue < 80 Then '
                            c.BgColor = Color.PapayaWhip.Name
                            c.InnerHtml = String.Format("<font color='{0}'>{1}</font>", Color.Magenta.Name, strItem)
                        End If
                        If iValue < 0 Then '
                            c.InnerText = ""
                            c.BgColor = Color.PapayaWhip.Name
                        End If
                    Catch FormatException As Exception
                        c.InnerText = "error!"
                        c.BgColor = Color.Red.Name
                    End Try
                End If
            Next
0
juststeveAuthor Commented:
Thanks ihenry...that logic lead me to see that the problem was the range of the iterator. When I implemented this with a datagrid i used:
For i As Int16 = 1 To 22

to skip the first column and include the rest.

But with the datarepeater the '1' _was including the first column and the '22' was only including half of the total number of columns. So clearly the datarepeat is doubling up on something. But initial tests look like everything's working now.

mmarinov - my header row problem is explained here: http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21206233.html

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.