Object reference not set to an instance of an object

B

Ben

Hi,

I want to access in code-behind a label within the ItemTemplate of a
Formview.

<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>

code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text


I get the error: "Object reference not set to an instance of an object"

Could somebody tell me what's wrong in my code?
Thanks
Ben
 
K

Konstantinos Pantos

Your template has not been instansiated yet.
Try running your code in the FormView DataBound event.
Always check that FindControl method returns some control before calling
properties on it.

HTH,

--
Konstantinos Pantos,
Software Engineer
Microsoft MVP [ASP.NET]
http://kostas.pantos.name
http://blog.pantos.name
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top