How do I set an object Reference?

  • Thread starter Thread starter jm
  • Start date Start date
J

jm

Dim order_type As DropDownList = _
(CType(e.Item.FindControl("order_type"), DropDownList))


This control is in a datalist itemtemplate. It will not find it.
Whenever I try and do anything further with this, it says:

Object reference not set to an instance of an object.

My guess is that that means it cannot find the control on the form.

Thank you for help.

If I change it to this:


Dim test As dropdownlist
test = CType(e.Item.FindControl("order_type"), dropdownlist)

I get no errors. But when I do this:

response.write(test.ID)

I get:

Object reference not set to an instance of an object.

Any ideas?

Finally,

e As DataListItemEventArgs)

Dim test As new dropdownlist
test = e.Item.FindControl("order_type")

response.write(test.ID)

I read the error a little close and put the new, but it still won't
find the control.

Thanks for any help.

It's in the OnItemDataBound event, so I don't know why it cannot find
the object (in a datalist control). I have tried everything I know.
Thank you for any help.
 
jm said:
Dim order_type As DropDownList = _
(CType(e.Item.FindControl("order_type"), DropDownList))


This control is in a datalist itemtemplate. It will not find it.
Whenever I try and do anything further with this, it says:

Object reference not set to an instance of an object.

My guess is that that means it cannot find the control on the form.

Thank you for help.

If I change it to this:


Dim test As dropdownlist
test = CType(e.Item.FindControl("order_type"), dropdownlist)

I get no errors. But when I do this:

response.write(test.ID)

I get:

Object reference not set to an instance of an object.

Any ideas?

Finally,

e As DataListItemEventArgs)

Dim test As new dropdownlist
test = e.Item.FindControl("order_type")

response.write(test.ID)

I read the error a little close and put the new, but it still won't
find the control.

Thanks for any help.

It's in the OnItemDataBound event, so I don't know why it cannot find
the object (in a datalist control). I have tried everything I know.
Thank you for any help.

In the OnItemDataBound event, you have to check first whether the
ListItemType is Item or AlternatingItem.
You get the error because the OnItemDataBound event is first
executed for the HeaderTemplate, which doesn't contain your
control.

Use something like this:
Sub DataList1_ItemDataBound(sender As Object, _
e As DataListItemEventArgs)
If(e.Item.ItemType=ListItemType.Item Or _
e.Item.ItemType=ListItemType.AlternatingItem) Then
' your code here
End If
 
Jos said:
In the OnItemDataBound event, you have to check first whether the
ListItemType is Item or AlternatingItem.
You get the error because the OnItemDataBound event is first
executed for the HeaderTemplate, which doesn't contain your
control.

Use something like this:
Sub DataList1_ItemDataBound(sender As Object, _
e As DataListItemEventArgs)
If(e.Item.ItemType=ListItemType.Item Or _
e.Item.ItemType=ListItemType.AlternatingItem) Then
' your code here
End If

Thank you Jos. It worked. I was wondering if you could recommend a
site or book that might get detailed like this instead of learning
everything by the set of my pants. Thanks again.
 
Back
Top