Can't FindControl in DataGrid

J

Jim Bayers

I get this error:

System.NullReferenceException: Object reference not set to an instance
of an object.

And this is the subroutine that throws it:


Protected Sub DataUpdate(ByVal Sender As Object, _
ByVal E As DataGridCommandEventArgs)

Label1.Text = CType(E.Item.FindControl("DropDownList1"),
DropDownList).SelectedItem.Text

' Save TempValue to DB
End Sub

The dropdownlist is in a datagrid template:

<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="False" oncancelcommand="DataCancel"
onupdatecommand="DataUpdate" datakeyfield="tourdate"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="3" GridLines="Horizontal"
oneditcommand="DataEdit">
<Columns>
<asp:EditCommandColumn
ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<asp:TemplateColumn
HeaderText="EditColumn">
<ItemTemplate>
<asp:Label id=Label2
runat="server" Text='<%# Container.DataItem("tourdate") %>'>Label
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
id=DropDownList1 runat="server" DataSource="<%# dbView %>"
DataTextField="tourDate" DataMember="spTours" DataValueField="id">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="tourDate"
HeaderText="Date"></asp:BoundColumn>
</Columns>
</asp:datagrid>

Everything else is working, the datagrid shows the data. The dropdown
box is populated. I can pick a value. But when I click the update link
on the datagrid, it throws the error.
 
J

Jim Bayers

I changed it to:

Sub UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.UpdateCommand
Dim ddl As New DropDownList()
ddl = e.Item.FindControl("DropDownList1")
Label1.Text = ddl.SelectedItem.Text
End Sub


and I get the same error:

System.NullReferenceException: Object reference not set to an instance
of an object.
 

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

Top