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.

Recommended Answers

All 2 Replies

Please try this:

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.Text = "False" Then
        Active.Text = "Public"
    Else
        Active.Text = "Private"
    End If
End Sub

try below code for object reference problem

If Active.Text = "False" Then
Active.Text = "Public"
Else
Active.Text = "Private"
End If

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.