DataGrid.FindControl() returns nothing

Discussion in 'ASP .Net' started by MattB, Feb 15, 2005.

  1. MattB

    MattB Guest

    I've got some controls (mostly textboxes for now) that get created at
    runtime in a DataGrid. I create them using the OnItemDataBound event. I
    realize this isn't ideal, but I'm trying to see if I can make this work
    anyway.

    In a button click event I loop through the dataset the datagrid is bound
    to and try to use FindControl to get at my controls. Below is a snippet
    from the button click event:

    iRow = 0
    For Each dr In dtGfields.Rows
    strCol = dr("ColName")
    theCell = dgGuest.Items(iRow).Cells(1)
    Select Case strCol
    ...
    Case Else
    tb = theCell.Controls(0)
    'dgGuest.FindControl(strCol)
    If UCase(dr("val")) <> UCase(tb.Text) Then
    xml = wwSales.EZTag(strCol, UCase(tb.Text))
    End If
    End Select
    ...
    iRow += 1
    Next

    I've tried a bunch of different approaches with FindControl but I always
    wind up with a return value of Nothing. Can anyone see anything here
    that I'm doing wrong? I initially started out with using it like this:

    tb = dgGuest.FindControl(strCol)

    TIA!
    Matt
     
    MattB, Feb 15, 2005
    #1
    1. Advertising

  2. MattB

    Wilco Bauwer Guest

    FindControl will only search for a control with the specified ID within
    the naming container of the control on which you called FindControl.
    You will have to narrow the search by getting to the container of the
    control you are looking for first.
     
    Wilco Bauwer, Feb 15, 2005
    #2
    1. Advertising

  3. MattB

    Scott Allen Guest

    Hi Matt:

    You'll want to call FindControl on the rows of the DataGrid and not
    the DataGrid itself. A DataGrid row is a DataGridItem.

    I have some samples in C# here:
    http://odetocode.com/Articles/116.aspx

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

    On Tue, 15 Feb 2005 15:32:55 -0700, MattB <>
    wrote:

    >I've got some controls (mostly textboxes for now) that get created at
    >runtime in a DataGrid. I create them using the OnItemDataBound event. I
    >realize this isn't ideal, but I'm trying to see if I can make this work
    >anyway.
    >
    >In a button click event I loop through the dataset the datagrid is bound
    >to and try to use FindControl to get at my controls. Below is a snippet
    >from the button click event:
    >
    > iRow = 0
    > For Each dr In dtGfields.Rows
    > strCol = dr("ColName")
    > theCell = dgGuest.Items(iRow).Cells(1)
    > Select Case strCol
    > ...
    > Case Else
    > tb = theCell.Controls(0)
    > 'dgGuest.FindControl(strCol)
    > If UCase(dr("val")) <> UCase(tb.Text) Then
    > xml = wwSales.EZTag(strCol, UCase(tb.Text))
    > End If
    > End Select
    > ...
    > iRow += 1
    > Next
    >
    >I've tried a bunch of different approaches with FindControl but I always
    >wind up with a return value of Nothing. Can anyone see anything here
    >that I'm doing wrong? I initially started out with using it like this:
    >
    > tb = dgGuest.FindControl(strCol)
    >
    >TIA!
    >Matt
     
    Scott Allen, Feb 16, 2005
    #3
    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. James G. Beldock
    Replies:
    1
    Views:
    9,385
    Kevin Spencer
    Aug 7, 2003
  2. Rimu Atkinson
    Replies:
    2
    Views:
    3,062
    Rimu Atkinson
    Feb 3, 2004
  3. steroche

    FindControl Method always returns empty string!!!!!

    steroche, Aug 25, 2005, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    164
    steroche
    Aug 25, 2005
  4. steroche

    FindControl Method always returns empty string!!!!!

    steroche, Aug 25, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    158
    steroche
    Aug 25, 2005
  5. Devin Jeanpierre
    Replies:
    2
    Views:
    535
    Devin Jeanpierre
    Feb 14, 2012
Loading...

Share This Page