locked
FindControl() does not work on CompositeControls RRS feed

  • Question

  • User-665773464 posted

    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?

    Thank you.

     

     

     

     

    Friday, August 29, 2008 1:27 PM

Answers

  • User-665773464 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 29, 2008 3:58 PM

All replies

  • User-1573490007 posted

    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?

    Friday, August 29, 2008 1:54 PM
  • User-665773464 posted

    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

    Thanks

    Friday, August 29, 2008 2:10 PM
  • User-1573490007 posted

    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.

    Friday, August 29, 2008 2:12 PM
  • User-665773464 posted

     Doesn't get much more straight forward than the code snippet below.  It's got to be a bug in the framework.

     Dim oCtrl As Control
    ' DisplayControl is a references to a UserControl
    ' This line of code yeilds oCtrl = Nothing
    oCtrl = DisplayControl.FindControl("ctrCityFileLookup")

    ' This loop finds the control Okay.
    For Each oCtrl In DisplayControl.Controls
    If oCtrl.ID = "ctrCityFileLookup" Then
    ctrCityFileLookup = DirectCast(oCtrl, WebCityInfoLookup)
    End If

    Next

    Friday, August 29, 2008 2:29 PM
  • User-665773464 posted

     Doesn't get much more straight forward than the code snippet below.  It's got to be a bug in the framework.

     Dim oCtrl As Control
    ' DisplayControl is a references to a UserControl
    ' This line of code yeilds oCtrl = Nothing
    oCtrl = DisplayControl.FindControl("ctrCityFileLookup")

    ' This loop finds the control Okay.
    For Each oCtrl In DisplayControl.Controls
     f oCtrl.ID = "ctrCityFileLookup" Then
    ctrCityFileLookup = DirectCast(oCtrl, WebCityInfoLookup)
    End If

    Next

     
    Friday, August 29, 2008 2:29 PM
  • User-665773464 posted

     Doesn't get much more straight forward than the code snippet below.  It's got to be a bug in the framework.

     Dim oCtrl As Control
    ' DisplayControl is a references to a UserControl
    ' This line of code yeilds oCtrl = Nothing
    oCtrl = DisplayControl.FindControl("ctrCityFileLookup")

    ' This loop finds the control Okay.
    For Each oCtrl In DisplayControl.Controls
     f oCtrl.ID = "ctrCityFileLookup" Then
    ctrCityFileLookup = DirectCast(oCtrl, WebCityInfoLookup)
    End If

    Next

     
    Friday, August 29, 2008 2:29 PM
  • User-1573490007 posted
    The framework has bugs, but they're (for the most part) in far deeper corners of it, so I'd find that highly unlikely. My suggestion however was to set a breakpoint in the method that works (the slower one that drills down the page), and to F11 in it until it finds the control. Then look at what was its parent, which will clue you as to what happened. FindControl is FindControl, so if one works but not the other, there's a logistic error at work, not a technical one.
    Friday, August 29, 2008 2:33 PM
  • User-665773464 posted

    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.

    Friday, August 29, 2008 2:38 PM
  • User-1573490007 posted

    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.

    Friday, August 29, 2008 2:54 PM
  • User-665773464 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 29, 2008 3:58 PM
  • User-1573490007 posted

    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!

    Friday, August 29, 2008 4:00 PM