![]() |
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. |
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.