Search code examples
vb.netformviewreplacedatabound

How can I use the ItemCreated event to update the text of a label control contained in a formview?


How can I replace the text of a label control contained in a formview?

I've tried to use the code below:

Protected Sub FormViewNews_DataBound(sender As Object, e As System.EventArgs) Handles FormViewNews.DataBound
    Dim pagerRow As FormViewRow = FormViewNews.BottomPagerRow

    Dim Active As Label = CType(pagerRow.Cells(2).FindControl("OFMLabel"), Label)

    If Active = "False" Then
        Active = "Public"
    Else
        Active = "Private"
    End If
End Sub

But then I've got this error message: "Object reference not set to an instance of an object.".

i.e. I have a label named 'OFMLabel', and its valued 'TRUE'. Then, if TRUE, I want to change its text become 'PRIVATE', and if FALSE, become 'PUBLIC'.

Anyone can help me, please?

Thanks.

This problem solved with the code below:

SOLVED

    If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.Cells(2).Text = "False" Then
            e.Row.Cells(2).Text = "Public"
        Else
            e.Row.Cells(2).Text = "Private"
        End If
    End If

Thanks :)


Solution

  • The problem solved using the code below:

    If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.Cells(2).Text = "False" Then
            e.Row.Cells(2).Text = "Public"
        Else
            e.Row.Cells(2).Text = "Private"
        End If
    End If