Search code examples
vb.net-2010datarepeaterondrawitem

Retrieving .Text of Textbox located on Panel during DataRepeater_DrawItem event


I'm using the DrawItem event of a DataRepeater to change the .BackColor and .ForeColor of a textbox based on the .Text contents. This works just fine UNLESS the textbox is located on a panel. If the textbox is on a panel, then I am returning this exception: "Object reference not set to an instance of an object." It indicates that "e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).Text" doesn't have a value. Stepping through the code confirms it.

BUT... if I just slide that textbox off the panel, then it works just fine. My Google-Fu has failed me. What am I missing?

Additional Info: Visual Studio 2010 Professional, VB.Net targeting.NET 4.0

        'Set Record Status Color
        Select Case e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).Text
            Case "Working"
                e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).BackColor = Color.Green
                e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).ForeColor = Color.White
            Case "Sleep"
                e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).BackColor = Color.Red
                e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).ForeColor = Color.White
            Case Else
                e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).BackColor = Color.White
                e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).ForeColor = Color.Black
        End Select

Solution

  • Answer to my own question in case this puzzles someone else in the future:

    When a control is located on a panel, it is nested inside of it from a code perspective. So you have to reference the panel control, and then the control you are actually trying to change. So I had to chase my statements to read like this:

    e.DataRepeaterItem.Controls(panel_RecordDetails.Name).Controls(txt_AWQRecordStatus.Name).ForeColor = Color.Black
    

    Where panel_RecordDetails is the panel, and txt_AWQRecordStatus is the textbox on that panel. There may be an easier way to do this, but I'm going to take the win and move on.