Get reference to control in gridview at runtime...

H

huntco

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?
 
S

Sergey Poberezovskiy

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;
}
}
 
H

huntco

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.
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top