Last post Aug 29, 2008 04:00 PM by shados
Aug 29, 2008 01:27 PM|stujensen|LINK
I'm having a problem with the FindControl() method not finding a server control I created that inherits from CompositeControl.
The Composite Server control is on a UserControl which is on a form. I am not using master pages.
Dim oCtrl as Controls
oCtrl = MyUserControl.FindControl("MyCompositeControl") returns nothing.
Yet the same code will find all the other adjacent controls in the same table. Some of which are my own controls that either subclass other webcontrols or inherit from control.
I tracked down the physical index of the composite controls and found this code works
oCtrl = MyUserControl.Controls.Item(38), so the control is in the collection.
I also have my own recursive GetControl() method that starts at the page level and drills down the control tree using enumeration i.e. For Each Control in Controls and this code also works and finds the CompositeControl where I would expect it too and the
parent control is MyUserControl. I also looked at the trace dump and the CompositeControl does appear at the same level of all the other controls that FindControl() method works on. That verifies the composite control is not contained in some other controls
collection. I can work around this by using my recursive GetControl() but there is a small performance hit with that. If I know the control is at the same level I hate to have to start at the page and drill the entire control tree.
Anyone have any ideas?
Aug 29, 2008 01:54 PM|shados|LINK
FindControl is, as you already know, not recursive. The fact that your GetControl finds it, but FindControl does not, lead me to think that your composite control is inside some kind of container OR that its ID is not what you think it is, potentially set
programmatically or something...
Would either of these scenarios ring a bell?
Aug 29, 2008 02:10 PM|stujensen|LINK
I agree it's behaving as if it's in another controls collection, but it's not. Otherwise MyControl.Controls.Item(38) would not yeild that control. The control Id is correct as MyControl.Controls.Item(38).ID = the Control id I'm searching for including
matching the CaSe.
If I write code to enumerate the MyControl.Collection again I find the control
Aug 29, 2008 02:12 PM|shados|LINK
Thats puzzling. Did you debug your recursive method to see at which point in finds your control? It has to be using FindControl internally...so did you look on which control it calls FindControl and finally finds it? It may give you a clue.
Aug 29, 2008 02:29 PM|stujensen|LINK
Doesn't get much more straight forward than the code snippet below. It's got to be a bug in the framework.
Aug 29, 2008 02:33 PM|shados|LINK
Aug 29, 2008 02:38 PM|stujensen|LINK
I did that before posting this. The Parent to the Composite Control is the UserControl that it's on. There is no other parent to the control. I even called FindControl() on the parent control of my control in the debugger and it yeilded nothing.
FYI: I have no idea why my code sample posted several times. The page errored out when I clicked to post button....sorry folks.
Aug 29, 2008 02:54 PM|shados|LINK
The site is error-ing out a lot right now, but even if it does the posting operation still works.
And sorry, I may be confused about your explainations... in one of your original posts, you stated that you had a recursive FindControl-like method that -did- work.
This method almost certainly use FindControl internally. And it finds it. So FindControl does work, its just not calling it on the same thing as you are manually (or something similar), unless I'm mistaken about what you're saying. So the trick here would
be to figure out in which situation in DOES work, instead of which situation it doesn't.
Aug 29, 2008 03:58 PM|stujensen|LINK
Found the problem, my bad. I was overriding the control ID property and never delagated the value being set to the base control. FindControl is using type Control.Id which of course would not be set since I subclassed it. Boy, that was a painful mistake...thanks
for your help.
Aug 29, 2008 04:00 PM|shados|LINK
lead me to think that your composite control is inside some kind of container OR
that its ID is not what you think it is, potentially set programmatically or something...
Hahaha... I had it right in the first place :) Hey, it happens!