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. Advertisements

  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. Advertisements

  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 Allen, Feb 16, 2005
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.