Problems with UserControls within a DataGrid

Discussion in 'ASP .Net Datagrid Control' started by Anthony Williams, Sep 18, 2003.

  1. 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
     
    Anthony Williams, Sep 18, 2003
    #1
    1. Advertising

  2. 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" <>
    | 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: <>
    | 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
    |
    |
    |
     
    Jeffrey Tan[MSFT], Sep 19, 2003
    #2
    1. Advertising

  3. Anthony Williams

    robert deppe Guest

    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!
     
    robert deppe, Sep 19, 2003
    #3
  4. 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" <> wrote in message
    news:ebjJn$...
    > 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!
     
    Anthony Williams, Sep 22, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ujjc001
    Replies:
    0
    Views:
    1,371
    ujjc001
    Jul 27, 2005
  2. Ibana
    Replies:
    0
    Views:
    332
    Ibana
    Oct 3, 2006
  3. Paul Reilly

    problems with listbox id changing within datagrid control

    Paul Reilly, Jun 21, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    141
    Rick Spiewak
    Jun 23, 2004
  4. John

    DataGrid and UserControls

    John, Dec 4, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    105
    Axel Dahmen
    Dec 6, 2003
  5. Keith Patrick

    UserControls via LoadControl within a TreeView

    Keith Patrick, Dec 5, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    135
    Keith Patrick
    Dec 7, 2005
Loading...

Share This Page