Saturday, March 24, 2012

Object reference not set to an instance of an object

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
BenTry
FormView1.Row.FindControl(...)
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Ben" <b@.bn> wrote in message
news:%23obfXb8tHHA.3364@.TK2MSFTNGP02.phx.gbl...
> 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
>
>
On Jun 26, 2:06 pm, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@.mMvVpPsS.org> wrote:
> Try
> FormView1.Row.FindControl(...)
> --
> Eliyahu Goldin,
> Software Developer & Consultant
> Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
> "Ben" <b@.bn> wrote in message
> news:%23obfXb8tHHA.3364@.TK2MSFTNGP02.phx.gbl...
>
>
>
>
>
>
hi
,
if (FormView1.DefaultMode == FormViewMode.ReadOnly)
{
Label lbl = (Label) FormView1.FindControl("nameLabel");
}
thanks
Masudur
As a general rule, it's a good idea to break up the statements in a single
line multi-expression code line into their own lines of code to facilitate
setting breakpoints and being able to examine values. The way you have it
now, it would be extremely difficult to tell what is going on.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com
"Ben" wrote:

> 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
>
>

0 comments:

Post a Comment