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
 
M

Masudur

Try

FormView1.Row.FindControl(...)

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net


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

hi
,

if (FormView1.DefaultMode == FormViewMode.ReadOnly)
{
Label lbl = (Label) FormView1.FindControl("nameLabel");
}

thanks
Masudur
 

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,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top