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 
        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

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.

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.
derekhunzikerAuthor Commented:

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?


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

derekhunzikerAuthor Commented:
Anyone know how to access the DataItem for a specific field?
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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 --
   Dim booValue as Boolean = CBool(oValue)
   '--Do stuff when the value is true or false
End If

Open in new window


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
derekhunzikerAuthor Commented:

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!


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.
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

From novice to tech pro — start learning today.