?
Solved

Diff between DataGrid and Repeater's ItemDataBound event

Posted on 2004-11-14
11
Medium Priority
?
410 Views
Last Modified: 2012-08-13
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'.

0
Comment
Question by:juststeve
  • 6
  • 3
  • 2
11 Comments
 
LVL 20

Expert Comment

by:ihenry
ID: 12581341
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12581583
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
 

Author Comment

by:juststeve
ID: 12582551
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Accepted Solution

by:
ihenry earned 1600 total points
ID: 12582688
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
 
LVL 20

Expert Comment

by:ihenry
ID: 12582764
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
 

Author Comment

by:juststeve
ID: 12582923
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
 
LVL 20

Expert Comment

by:ihenry
ID: 12582951
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
 
LVL 20

Expert Comment

by:ihenry
ID: 12582971
>> 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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12583010
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
 
LVL 20

Expert Comment

by:ihenry
ID: 12583040
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
 

Author Comment

by:juststeve
ID: 12583206
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question