Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Problems with UserControls within a DataGrid

Reply
Thread Tools

Problems with UserControls within a DataGrid

 
 
Anthony Williams
Guest
Posts: n/a
 
      09-18-2003
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("StockListIte m"),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


 
Reply With Quote
 
 
 
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      09-19-2003

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" <(E-Mail Removed)>
| 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: <(E-Mail Removed)>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| NNTP-Posting-Host: host81-137-71-124.in-addr.btopenworld.com 81.137.71.124
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridc ontrol:6665
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
|
| 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("StockListIte m"),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
|
|
|

 
Reply With Quote
 
 
 
 
robert deppe
Guest
Posts: n/a
 
      09-19-2003
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.



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Anthony Williams
Guest
Posts: n/a
 
      09-22-2003
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



"robert deppe" <(E-Mail Removed)> wrote in message
news:ebjJn$(E-Mail Removed)...
> 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.
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems with Button Events in dynamic usercontrols Ibana ASP .Net 0 10-03-2006 06:53 PM
UserControls via LoadControl within a TreeView Keith Patrick ASP .Net Web Controls 2 12-07-2005 08:44 PM
Loading usercontrols, viewstate problem, slighly different from all others "viewstate uc problems" please help... ujjc001 ASP .Net 0 07-27-2005 01:52 PM
Editing a datagrid nested within another datagrid uk00121 ASP .Net Datagrid Control 2 04-19-2004 02:17 PM
DataGrid and UserControls John ASP .Net Web Controls 1 12-06-2003 04:29 PM



Advertisments