954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with FormView.FindControl

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

Ana D.
Junior Poster in Training
77 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Ramesh S
Posting Pro
583 posts since Jun 2009
Reputation Points: 165
Solved Threads: 113
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: