Problems with UserControls within a DataGrid

A

Anthony Williams

Afternoon all,

I'm having a slight issue with a user control that I've written for an
application. The DataGrid I'm using has a single TemplateColumn inside which
I have placed my control with id="StockListItem". The datagrid binds without
problems, and the controls are all sitting there happily, waiting to be
modified by using the .ItemDataBound event.

I have a custom class behind the control called StockListItemControl, which
inherits System.Web.UI.UserControl, which allows me to have properties for
each of the custom controls which are in the datagrid, so that I can modify
these properties in the ItemDataBound event.

This is where I'm starting to have fun with null reference exceptions. I'm
trying DESPERATELY to get hold of the controls once they're inside the
datagrid. After a bit of looping around the .Controls collection, I've
managed to get hold of the container in which my StockListItemControl
resides:


' -- Handle the ItemDataBound event to modify control properties
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound

' Find the control with ID="StockListItem"
Dim Ctrl As StockListItemControl =
CType(e.Item.Controls(0).FindControl("StockListItem"),StockListItemControl)

' And modify it's properties
Ctrl.Price = "Some price as a string"
Ctrl.Mileage = "Some mileage"
Ctrl.Make = "Some make"

End Sub
' -- End code -- '


I keep getting the error whenever I try and reference Ctrl: "Object
reference not set to an instance of an object."

Any ideas please?

Cheers,
Anth
 
J

Jeffrey Tan[MSFT]

Hi Anthony,

"Object reference not set to an instance of an object." means that
some reference of your program did not refer to an initialized object.

You can set a breakpoint in your application before the exception, then
check which reference does not refer properly.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Anthony Williams" <[email protected]>
| Subject: Problems with UserControls within a DataGrid
| Date: Thu, 18 Sep 2003 14:25:52 +0100
| Lines: 47
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: host81-137-71-124.in-addr.btopenworld.com 81.137.71.124
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:6665
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| Afternoon all,
|
| I'm having a slight issue with a user control that I've written for an
| application. The DataGrid I'm using has a single TemplateColumn inside
which
| I have placed my control with id="StockListItem". The datagrid binds
without
| problems, and the controls are all sitting there happily, waiting to be
| modified by using the .ItemDataBound event.
|
| I have a custom class behind the control called StockListItemControl,
which
| inherits System.Web.UI.UserControl, which allows me to have properties for
| each of the custom controls which are in the datagrid, so that I can
modify
| these properties in the ItemDataBound event.
|
| This is where I'm starting to have fun with null reference exceptions. I'm
| trying DESPERATELY to get hold of the controls once they're inside the
| datagrid. After a bit of looping around the .Controls collection, I've
| managed to get hold of the container in which my StockListItemControl
| resides:
|
|
| ' -- Handle the ItemDataBound event to modify control properties
| Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
| System.Web.UI.WebControls.DataGridItemEventArgs) Handles
| DataGrid1.ItemDataBound
|
| ' Find the control with ID="StockListItem"
| Dim Ctrl As StockListItemControl =
|
CType(e.Item.Controls(0).FindControl("StockListItem"),StockListItemControl)
|
| ' And modify it's properties
| Ctrl.Price = "Some price as a string"
| Ctrl.Mileage = "Some mileage"
| Ctrl.Make = "Some make"
|
| End Sub
| ' -- End code -- '
|
|
| I keep getting the error whenever I try and reference Ctrl: "Object
| reference not set to an instance of an object."
|
| Any ideas please?
|
| Cheers,
| Anth
|
|
|
 
R

robert deppe

Try

Dim ctl As StockListItemControl=
DirectCast(e.Item.FindControl("StockListItem"), StockListItemControl)

Jeff is right. Your 'find' didn't find the control. Notice that I left
the controls collection part off.

The DirectCast is not important, just a another (better) way to do it.

Good Luck.
 
A

Anthony Williams

Robert/Jeff,

Thanks for the help - though when I was unable to find the control, I
ditched the idea of keeping the control in the .aspx file and switched to
using LoadControl(...) on the DataBound event.

Robert - I did try the e.Item.FindControl and that didn't work either,
though I didn't try using the DirectCast method (instead, I used CType).

Next time I'm adding a control to a datagrid (which should be sometime this
week) I'll give it another shot using that method.

Cheers,
Anth
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top