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
Alex Meleta - 27 Jun 2007 11:52 GMT
Hi Ben,
The direct answer is: nothing wrong.
It's probably an issue outside given code. Checks FormView1.Row and FormView1.DataItem
are not null. If yes then be sure you have at least one record in FormView1's
source and DataBind is performed on the FormView1. You cannot retrieve controls
if they are not processed.
Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
B> FormView1.FindControl("nameLabel")
B>