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

StackOverflow https://stackoverflow.com/questions/8004563

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

有帮助吗?

解决方案 2

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

其他提示

I think you will need to say what you want to change. You cant change a label to private, you want the text to say private right?

If Active.text = "False" Then
Active.text = "Public"

maybe that? You want to check on something. dont forget to say WHAT you want to check. in this case its the text of your label right? Or did I get it all wrong?

Hope this could help you in any way.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top