Access WebControl in custom control

W

William Leary

I cannot access a server control that is nested in a
template control...like so:

<dtag:pageTemplate runat="server" id="myPageTemplate">
<MAINCONTENT><asp:placeHolder ID="quoteContent"
runat="server"></asp:placeHolder>
</MAINCONTENT>
</dtag:pageTemplate>

The contents of the MAINCONTENT tag are placed into the
page via the following in the PageTemplate class:

_mainContentContainer = new TemplateItem(this);
MainContent.InstantiateIn(_mainContentContainer);
_mainTableRow5Cell3.Controls.Add(_mainContentContainer);

Everything works great, except for when I'm using
WebControls and trying to access them in the code-behind
page. I cannot access them. I have a variable declared of
type PlaceHolder, but it is always null when I access it.
FindControl using "quoteContent" doesn't work from the
page, of from the myPageTemplate object.

I suspect this has something to do with naming
containers, but very mystified as to how to go about this
at all. Have searched online for several hours now, to
now avail. Any thoughts would be appreciated. Thanks.
 
W

William Leary

Found the answer! It's necessary to recrusively search
the controls on the page, as the control I made was
several levels down in a control collection. The
following function works:

public static object RecursiveFindControl(Control
control, string id)
{
object o = control.FindControl(id);

if (o != null) { return o; }
else
{
foreach (Control c in control.Controls)
{
o = RecursiveFindControl(c, id);

if (o != null) { return o; }
}

return null;
}
}
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top