Hi,

I have the following code:

<asp:FormView ID="myFormView" runat="server" SkinID="DetailsFormview">
            <ItemTemplate>
                <table width="400">
                    <tr>
                        <td align="right">
                            <b>This is the label: </b>
                        </td>
                        <td align="left">
                            <asp:Label ID="myLabel" Text="Label Text"
runat="server" />
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:FormView>

And in the code behind (VB.NET):

Protected Sub BindFormView()
    Dim myLabel As Label = myFormView.FindControl("myLabel")
    myLabel.Text = AnotherLabel.Text
End Sub

However, myLabel is always nothing. As you see, my formview has only an ItemTemplate. I tried setting hte DefaultMode to ReadOnly but I still have the same problem: myLabel = Nothing!

Any suggestion of what is wrong?

Thanks,

Ana

Dani AI

Generated

is correct: the controls declared inside a FormView template do not exist until the FormView has been bound and the template instance is created. Accessing FindControl too early (for example, before calling DataBind or before the FormView has finished data-binding) will return Nothing. (learn.microsoft.com)

Use the FormView lifecycle to locate the control. A reliable pattern is to handle the FormView DataBound (or ItemCreated) event and then find the control from the current row. Example (VB.NET):

Protected Sub myFormView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles myFormView.DataBound
Dim row As FormViewRow = myFormView.Row
If row IsNot Nothing Then
Dim myLabel As Label = CType(row.FindControl("myLabel"), Label)
If myLabel IsNot Nothing Then
myLabel.Text = AnotherLabel.Text
End If
End If
End Sub

This uses the FormView Row as the naming container for the active template, which is the correct place to call FindControl. (learn.microsoft.com)

Troubleshooting checklist:

  • Make sure the FormView actually has data (DataSource/DataBind or a DataSource control that returns rows).
  • Confirm the active template/mode is the one you expect (ItemTemplate shows in ReadOnly mode). (learn.microsoft.com)
  • Verify the control ID spelling and that you are searching the correct naming container — FindControl only searches within the current naming container (it does not automatically traverse master pages or other naming containers). (learn.microsoft.com)
  • Always null-check the result of FindControl before using it.

If the FormView is bound declaratively (SqlDataSource/ObjectDataSource), the DataBound event is the safest place to change template controls; if you bind manually, call DataBind() first and then locate the control. (learn.microsoft.com)

FormView is basically used to display the values of a single record from a data source.

The controls in the ItemTemplate will be rendered only after binding a data source to the FormView control.

In the BindFormView() method, you didn't bind any data source to the FormView control. Therefore myFormView.FindControl("myLabel") returns always nothing.

Try after data binding to the FormView. It will return the Label control.

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.