I have the following code:

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

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?



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 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.