Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Building Controls (http://www.velocityreviews.com/forums/f59-asp-net-building-controls.html)
-   -   Access WebControl in custom control (http://www.velocityreviews.com/forums/t755918-access-webcontrol-in-custom-control.html)

William Leary 10-08-2003 06:55 PM

Access WebControl in custom control
 
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(_mainContentConta iner);

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.


William Leary 10-08-2003 08:28 PM

Access WebControl in custom control
 
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;
}
}


All times are GMT. The time now is 08:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57