How do I access data in a DataItem of repeater row on the ItemCreated event?

Hi There,

I have a boolean field in my SQL database that is displayed in a repeater as the text of a LinkButton.  I want the entire repeater row's background color to change if the value of that field is True.  I have tried to access the value from both the DataItem and the control itself on the ItemCreated event with no luck.  Here is what I have:


Protected Sub repeater_ItemCreated(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
 
         ' If the item is an alternating item, change the background color
        If e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim ThisRow As HtmlTableRow
            ThisRow = e.Item.FindControl("BodyRow")
            ThisRow.Attributes.Add("bgcolor", "#F0F5FE")
        End If
 
        ' If the row contains True for my database field, change the background 
        
        'GET "TRUE" OR "FALSE" VALUE FROM CONTROL OR DATAITEM????
 
        If ***** IT'S TRUE ***** Then
            Dim ThisRow As HtmlTableRow
            ThisRow = e.Item.FindControl("BodyRow")
            ThisRow.Attributes.Add("bgcolor", "#FF0000")
        End If
 
    End Sub

Open in new window

derekhunzikerAsked:
Who is Participating?
 
GreymanMSCConnect With a Mentor Commented:
You've got the right idea.  


Dim vwItem As System.Data.DataRowView = CType(e.Item.DataItem, System.Data.DataRowView)
Dim oValue As Object = vwItem("columnName")
'--alternatively either:
'Dim oValue As Object = vwItem.Item("columnName")
'Dim oValue As Object = DataBinder.Eval(vwItem, "column_name")
'----
If IsDBNull(oValue) Then
  '-- Do stuff when the value is NULL --
Else
   Dim booValue as Boolean = CBool(oValue)
   '--Do stuff when the value is true or false
End If

Open in new window

0
 
GreymanMSCCommented:
You cannot access it there as it does not yet exist!

The ItemCreated event fires *before* the DataItem property is assigned.

You need to place the code inside the ItemDataBound event, which fires *after* the DataItem is assigned.
0
 
derekhunzikerAuthor Commented:
GreymanMSC,

Thanks very much for the info.  I will go ahead and give that a shot.  In addition to having the code in the wrong place, I fear I may be accessing the DataItem incorrectly.  

In the ItemDataBound event,  would I access it like this?

e.Item.DataItem("column_id")

Can you give me an example of how to get the boolean value into a simple if statement?

Thanks!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
derekhunzikerAuthor Commented:
Anyone know how to access the DataItem for a specific field?
0
 
derekhunzikerAuthor Commented:
Thanks!!!

At first, the CType on the first line didn't take:

ERROR: Unable to cast object of type 'System.Data.Common.DbDataRecord' to type 'System.Data.DataRowView'.

Changing it to the following did the trick (It must be because I'm using a repeater???)

Dim vwItem As System.Data.Common.DbDataRecord = CType(e.Item.DataItem, System.Data.Common.DbDataRecord)

Also, the last oValue was the one that worked for me.

Your a life saver!

Cheers

0
 
GreymanMSCCommented:
Ah, I see.  It looks as if you are using a different datasource than I typically do.  (SqlDatasoure)  I did not think of that possibility.  But, at least you figured out what was happening easily enough.
0
All Courses

From novice to tech pro — start learning today.