Get reference to control in gridview at runtime...

Discussion in 'ASP .Net Building Controls' started by huntco, Jul 3, 2007.

  1. huntco

    huntco Guest

    I have a gridview control with a headertemplate (and other
    templates). In that headertemplate, I have some controls (a textbox,
    for example, used to filter the data in the gridview). When the user
    clicks a button on the page (the "filter" button), I need to grab the
    value (text) of that control - but I'm having a difficult time getting
    a reference to it.

    FindControl("controlName") doesn't do it. I've tried iterating
    through all the controls (recursively) looking for one with the
    "controlName" as the ID, but I've hit recursion "infinite loop"
    warnings. Scoping for particular object types (to cut down on the
    recursive loops) doesn't work either.

    How do I reference an object at runtime that's buried several layers
    within another object?
     
    huntco, Jul 3, 2007
    #1
    1. Advertisements

  2. inside GridView_RowCreated event:
    if (e.RowType == DataControlRowType.Header){
    // here you will need to reference the cell containing your textbox
    TableCell cell = e.Cells[myTextBoxCellIndex];
    foreach (Control control in cell.Controls){
    if (control is TextBox)
    string myText = control.Text;
    }
    }
     
    Sergey Poberezovskiy, Jul 3, 2007
    #2
    1. Advertisements

  3. huntco

    huntco Guest

    Thanks. Tweaked it a little bit, but that was the nudge in the right
    direction that I needed.

    I wound up creating private objects (textbox) in the class that
    parallel the objects I'm looking for in the gridview. Then "on page
    load," I cycle through gridview.headerrow like you have outlined
    (page_load vice rowCreated) and link each of the textbox controls to
    their corresponding private textboxes at the class level. That way I
    always have a reference to the "deeper" objects at the class level.

    Thanks again.
     
    huntco, Jul 3, 2007
    #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.