Need help with FindControl in DataGrid

Discussion in 'ASP .Net' started by Terry Olsen, May 2, 2005.

  1. Terry Olsen

    Terry Olsen Guest

    I've got a datagrid set up to display data. I've also got an
    Edit,Update,Cancel column set up to allow editing of data.

    I've got a DropDownList (ID="ddl3")in the EditItemTemplate for a certain
    column that I need to populate while in Edit Mode.

    Two questions: Is there a way to directly use another DropDownList as a
    DataSource? If so how?

    I'm using the following code to try to populate the DropDownList, but
    getting an "Object Reference not set to an Instance of an Object" error
    when trying to add items to it. The column is the 4th column (index=3).

    If I remove the code after the "LoadGrid()" line, then everything
    populates fine except for the DropDownList, which is empty.

    Hopefully someone can help me get this code working.

    -----------------------------------------
    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    DataGrid1.EditCommand
    DataGrid1.EditItemIndex = e.Item.ItemIndex
    btnAddServer.Visible = False
    LoadGrid()
    Dim ddl As DropDownList =
    CType(e.Item.Cells(3).FindControl("ddl3"), DropDownList)
    For i As Integer = 0 To ddlTechs.Items.Count - 1
    ddl.Items.Add(ddlTechs.Items.Item(i).Value)
    Next
    End Sub
    ----------------------------------------

    *** Sent via Developersdex http://www.developersdex.com ***
     
    Terry Olsen, May 2, 2005
    #1
    1. Advertising

  2. Terry Olsen

    Scott Allen Guest

    Hi Terry:

    One tip I can offer is to use FindControl on the Item instead of on a
    Cell. This allows the column to move and you don't need to change your
    code, i.e. e.Item.FindControl("..."). I have a little article with
    more info: http://odetocode.com/Articles/116.aspx

    Also, you might want to step through with the debugger and find out
    exactly what variable is undefined (Nothing).

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Sun, 01 May 2005 18:56:27 -0700, Terry Olsen <>
    wrote:

    >
    >I've got a datagrid set up to display data. I've also got an
    >Edit,Update,Cancel column set up to allow editing of data.
    >
    >I've got a DropDownList (ID="ddl3")in the EditItemTemplate for a certain
    >column that I need to populate while in Edit Mode.
    >
    >Two questions: Is there a way to directly use another DropDownList as a
    >DataSource? If so how?
    >
    >I'm using the following code to try to populate the DropDownList, but
    >getting an "Object Reference not set to an Instance of an Object" error
    >when trying to add items to it. The column is the 4th column (index=3).
    >
    >If I remove the code after the "LoadGrid()" line, then everything
    >populates fine except for the DropDownList, which is empty.
    >
    >Hopefully someone can help me get this code working.
    >
    >-----------------------------------------
    >Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
    >System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    >DataGrid1.EditCommand
    > DataGrid1.EditItemIndex = e.Item.ItemIndex
    > btnAddServer.Visible = False
    > LoadGrid()
    > Dim ddl As DropDownList =
    >CType(e.Item.Cells(3).FindControl("ddl3"), DropDownList)
    > For i As Integer = 0 To ddlTechs.Items.Count - 1
    > ddl.Items.Add(ddlTechs.Items.Item(i).Value)
    > Next
    > End Sub
    >----------------------------------------
    >
    >*** Sent via Developersdex http://www.developersdex.com ***
     
    Scott Allen, May 2, 2005
    #2
    1. Advertising

  3. Terry Olsen

    Terry Olsen Guest

    Thanks for the tip. I'll check it out and see what I can do.

    I moved the code over to the Update Event and it can find the control
    just fine. Does the control not exist until AFTER the DataGrid is
    rendered and we exit the Edit Event Handler? If so, how would I
    populate it so that the user can select an item from it before clicking
    the Update button?

    *** Sent via Developersdex http://www.developersdex.com ***
     
    Terry Olsen, May 2, 2005
    #3
  4. Terry Olsen

    Scott Allen Guest

    Hi Terry:

    Since you are pulling items from another drop down list it would
    depend on the order of events and when the items appear in the other
    list. Are you using databinding to populate ddlTechs?

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Mon, 02 May 2005 08:53:26 -0700, Terry Olsen <>
    wrote:

    >Thanks for the tip. I'll check it out and see what I can do.
    >
    >I moved the code over to the Update Event and it can find the control
    >just fine. Does the control not exist until AFTER the DataGrid is
    >rendered and we exit the Edit Event Handler? If so, how would I
    >populate it so that the user can select an item from it before clicking
    >the Update button?
    >
    >*** Sent via Developersdex http://www.developersdex.com ***
     
    Scott Allen, May 2, 2005
    #4
  5. Terry Olsen

    Terry Olsen Guest

    No. I'm pulling the items in ddlTechs from an SQL database, but ddlTechs is
    not bound to anything. ddlTechs is a drop down list that appears on the page
    outside of the Datagrid. When I want to edit a row inside the DataGrid, I
    want the DropDownList in the DataGrid to populate its items from the
    ddlTechs control. But I'm not getting that far.

    Response.write(Ctype(e.items.FindControl("ddl3").GetType.ToString)) <--
    Typed from memory, may not be correct.

    This works fine in the Update event and shows "DropDownList". But when I try
    the same thing in the Edit Event, I get the "Object Reference Not Set..."
    error.



    "Scott Allen" <> wrote in message
    news:...
    > Hi Terry:
    >
    > Since you are pulling items from another drop down list it would
    > depend on the order of events and when the items appear in the other
    > list. Are you using databinding to populate ddlTechs?
    >
    > --
    > Scott
    > http://www.OdeToCode.com/blogs/scott/
    >
    > On Mon, 02 May 2005 08:53:26 -0700, Terry Olsen <>
    > wrote:
    >
    >>Thanks for the tip. I'll check it out and see what I can do.
    >>
    >>I moved the code over to the Update Event and it can find the control
    >>just fine. Does the control not exist until AFTER the DataGrid is
    >>rendered and we exit the Edit Event Handler? If so, how would I
    >>populate it so that the user can select an item from it before clicking
    >>the Update button?
    >>
    >>*** Sent via Developersdex http://www.developersdex.com ***

    >
     
    Terry Olsen, May 2, 2005
    #5
  6. Terry Olsen

    Scott Allen Guest

    Hi Terry:

    Apologies for not getting thorugh to me, I can see now the edit event
    must fire before the drop down exists, then you reload the grid, but
    of course e.Item must still be pointing to the old item withought a
    drop down list.

    A solution would be to override ItemDataBound on the DataGrid, then
    when the edit item is being bound (e.Item.ItemType ==
    ListItemType.EditItem) you can FindControl on the list and populate
    it.

    Make sense?

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Mon, 2 May 2005 12:01:49 -0600, "Terry Olsen"
    <> wrote:

    >No. I'm pulling the items in ddlTechs from an SQL database, but ddlTechs is
    >not bound to anything. ddlTechs is a drop down list that appears on the page
    >outside of the Datagrid. When I want to edit a row inside the DataGrid, I
    >want the DropDownList in the DataGrid to populate its items from the
    >ddlTechs control. But I'm not getting that far.
    >
    >Response.write(Ctype(e.items.FindControl("ddl3").GetType.ToString)) <--
    >Typed from memory, may not be correct.
    >
    >This works fine in the Update event and shows "DropDownList". But when I try
    >the same thing in the Edit Event, I get the "Object Reference Not Set..."
    >error.
    >
    >
    >
    >"Scott Allen" <> wrote in message
    >news:...
    >> Hi Terry:
    >>
    >> Since you are pulling items from another drop down list it would
    >> depend on the order of events and when the items appear in the other
    >> list. Are you using databinding to populate ddlTechs?
    >>
    >> --
    >> Scott
    >> http://www.OdeToCode.com/blogs/scott/
    >>
    >> On Mon, 02 May 2005 08:53:26 -0700, Terry Olsen <>
    >> wrote:
    >>
    >>>Thanks for the tip. I'll check it out and see what I can do.
    >>>
    >>>I moved the code over to the Update Event and it can find the control
    >>>just fine. Does the control not exist until AFTER the DataGrid is
    >>>rendered and we exit the Edit Event Handler? If so, how would I
    >>>populate it so that the user can select an item from it before clicking
    >>>the Update button?
    >>>
    >>>*** Sent via Developersdex http://www.developersdex.com ***

    >>

    >
     
    Scott Allen, May 2, 2005
    #6
  7. Terry Olsen

    Terry Olsen Guest

    Well, just reading it here doesn't make sense. Let me go play with it and
    get back to you.

    "Scott Allen" <> wrote in message
    news:eek:...
    > Hi Terry:
    >
    > Apologies for not getting thorugh to me, I can see now the edit event
    > must fire before the drop down exists, then you reload the grid, but
    > of course e.Item must still be pointing to the old item withought a
    > drop down list.
    >
    > A solution would be to override ItemDataBound on the DataGrid, then
    > when the edit item is being bound (e.Item.ItemType ==
    > ListItemType.EditItem) you can FindControl on the list and populate
    > it.
    >
    > Make sense?
    >
    > --
    > Scott
    > http://www.OdeToCode.com/blogs/scott/
    >
    > On Mon, 2 May 2005 12:01:49 -0600, "Terry Olsen"
    > <> wrote:
    >
    >>No. I'm pulling the items in ddlTechs from an SQL database, but ddlTechs
    >>is
    >>not bound to anything. ddlTechs is a drop down list that appears on the
    >>page
    >>outside of the Datagrid. When I want to edit a row inside the DataGrid, I
    >>want the DropDownList in the DataGrid to populate its items from the
    >>ddlTechs control. But I'm not getting that far.
    >>
    >>Response.write(Ctype(e.items.FindControl("ddl3").GetType.ToString)) <--
    >>Typed from memory, may not be correct.
    >>
    >>This works fine in the Update event and shows "DropDownList". But when I
    >>try
    >>the same thing in the Edit Event, I get the "Object Reference Not Set..."
    >>error.
    >>
    >>
    >>
    >>"Scott Allen" <> wrote in message
    >>news:...
    >>> Hi Terry:
    >>>
    >>> Since you are pulling items from another drop down list it would
    >>> depend on the order of events and when the items appear in the other
    >>> list. Are you using databinding to populate ddlTechs?
    >>>
    >>> --
    >>> Scott
    >>> http://www.OdeToCode.com/blogs/scott/
    >>>
    >>> On Mon, 02 May 2005 08:53:26 -0700, Terry Olsen <>
    >>> wrote:
    >>>
    >>>>Thanks for the tip. I'll check it out and see what I can do.
    >>>>
    >>>>I moved the code over to the Update Event and it can find the control
    >>>>just fine. Does the control not exist until AFTER the DataGrid is
    >>>>rendered and we exit the Edit Event Handler? If so, how would I
    >>>>populate it so that the user can select an item from it before clicking
    >>>>the Update button?
    >>>>
    >>>>*** Sent via Developersdex http://www.developersdex.com ***
    >>>

    >>

    >
     
    Terry Olsen, May 3, 2005
    #7
  8. Terry Olsen

    Elton W Guest

    Hi Terry,

    It's little bit tricky problem when using FindControl in
    EditCommand event. You know that the editable DatagridItem
    has two states, normal and edit. In normal state, it shows
    stuff in ItemTemplate tags, on the other hand, in edit
    state, it shows stuff in EditItemTemplate tags.
    Accordingly using FindControl method you can only lookup
    corresponding controls. In EditCommand event, after

    DataGrid.EditItemIndex = e.Item.ItemIndex
    Datagrid.DataSource = data_source_object
    Datagrid.DataBind()

    The datagrid.Items(e.Item.ItemIndex) becomes edit item,
    but e.Item still keeps in normal state. Hence if you use
    datagrid.Items(e.Item.ItemIndex).FindControl("ddl3"), it
    will work.

    BTW, Scott's suggestion to use DataGridItem.FindControl is
    much better than use TableCell.FindControl, unless you
    have different controls with same ID in different cells.


    HTH

    Elton Wang




    >-----Original Message-----
    >
    >I've got a datagrid set up to display data. I've also

    got an
    >Edit,Update,Cancel column set up to allow editing of data.
    >
    >I've got a DropDownList (ID="ddl3")in the

    EditItemTemplate for a certain
    >column that I need to populate while in Edit Mode.
    >
    >Two questions: Is there a way to directly use another

    DropDownList as a
    >DataSource? If so how?
    >
    >I'm using the following code to try to populate the

    DropDownList, but
    >getting an "Object Reference not set to an Instance of an

    Object" error
    >when trying to add items to it. The column is the 4th

    column (index=3).
    >
    >If I remove the code after the "LoadGrid()" line, then

    everything
    >populates fine except for the DropDownList, which is

    empty.
    >
    >Hopefully someone can help me get this code working.
    >
    >-----------------------------------------
    >Private Sub DataGrid1_EditCommand(ByVal source As Object,

    ByVal e As
    >System.Web.UI.WebControls.DataGridCommandEventArgs)

    Handles
    >DataGrid1.EditCommand
    > DataGrid1.EditItemIndex = e.Item.ItemIndex
    > btnAddServer.Visible = False
    > LoadGrid()
    > Dim ddl As DropDownList =
    >CType(e.Item.Cells(3).FindControl("ddl3"), DropDownList)
    > For i As Integer = 0 To ddlTechs.Items.Count - 1
    > ddl.Items.Add(ddlTechs.Items.Item(i).Value)
    > Next
    > End Sub
    >----------------------------------------
    >
    >*** Sent via Developersdex http://www.developersdex.com

    ***
    >.
    >
     
    Elton W, May 3, 2005
    #8
  9. Terry Olsen

    Terry Olsen Guest

    You guys are awesome! I used both suggestions and got the routine
    working. Here is the code that works:

    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    DataGrid1.EditCommand
    DataGrid1.EditItemIndex = e.Item.ItemIndex
    btnAddServer.Visible = False
    LoadGrid()
    Dim ddl As DropDownList =
    CType(DataGrid1.Items(e.Item.ItemIndex).FindControl("ddl3"),
    DropDownList)
    For i As Integer = 0 To ddlTechs.Items.Count - 1
    ddl.Items.Add(ddlTechs.Items.Item(i).Value)
    Next
    End Sub

    Thanks again!

    *** Sent via Developersdex http://www.developersdex.com ***
     
    Terry Olsen, May 3, 2005
    #9
  10. Terry Olsen

    Scott Allen Guest

    Good deal, Terry, and good advice, Elton.

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Mon, 02 May 2005 18:48:19 -0700, Terry Olsen <>
    wrote:

    >You guys are awesome! I used both suggestions and got the routine
    >working. Here is the code that works:
    >
    >Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
    >System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    >DataGrid1.EditCommand
    > DataGrid1.EditItemIndex = e.Item.ItemIndex
    > btnAddServer.Visible = False
    > LoadGrid()
    > Dim ddl As DropDownList =
    >CType(DataGrid1.Items(e.Item.ItemIndex).FindControl("ddl3"),
    >DropDownList)
    > For i As Integer = 0 To ddlTechs.Items.Count - 1
    > ddl.Items.Add(ddlTechs.Items.Item(i).Value)
    > Next
    > End Sub
    >
    >Thanks again!
    >
    >*** Sent via Developersdex http://www.developersdex.com ***
     
    Scott Allen, May 3, 2005
    #10
  11. Terry Olsen

    Terry Olsen Guest

    Okay, now i've got my code completely working. I wish I better
    understood the scope of the various ways of doing it (cells vs. items,
    etc). Anyhow, here's the completed code for the Edit event. Notice the
    last 2 lines concerning the label. I'm taking the value from the label
    in the Normal Template and using it to change the SelectedValue in the
    DropDownList. Is this the "preferred" way, or is there a better way (as
    was pointed out earlier in this thread) of finding the Label Control?

    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    DataGrid1.EditCommand
    DataGrid1.EditItemIndex = e.Item.ItemIndex
    btnAddServer.Visible = False
    LoadGrid()
    Dim ddl As DropDownList =
    CType(DataGrid1.Items(e.Item.ItemIndex).FindControl("ddl3"),
    DropDownList)
    For i As Integer = 0 To ddlTechs.Items.Count - 1
    ddl.Items.Add(ddlTechs.Items.Item(i).Value)
    Next
    Dim lb As Label = CType(e.Item.Cells(3).FindControl("Label3"),
    Label)
    ddl.SelectedValue = lb.Text
    End Sub

    *** Sent via Developersdex http://www.developersdex.com ***
     
    Terry Olsen, May 3, 2005
    #11
    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. Sid
    Replies:
    4
    Views:
    606
  2. Dave

    Datagrid FindControl in Footer

    Dave, Nov 14, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    3,117
  3. Mike Speak

    DataGrid FindControl HELP...

    Mike Speak, Nov 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    661
    Mike Speak
    Nov 19, 2003
  4. Ruby

    findcontrol in datagrid footer

    Ruby, Jan 28, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,833
  5. ad

    When need to use FindControl

    ad, Apr 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    457
    Karl Seguin
    Apr 28, 2005
Loading...

Share This Page