views:

17

answers:

1

I have 2 user controls, I is used as a container for the other:

<mc:Container runat="server" ID="container">
   <mc:MyControl runat="server" ID="test">
</mc:Container>

The mc Container has a default inner property called content which is a collection of MyControls.

The markup above is inside a FormView, and when I call FindControl on the formview it can find the container, but it cannot find the test.

How can I make the container control not create a new Naming container?

EDIT__

When not in a FormView, the inner control's IDs do show up as part of the page in the designer, so there it is working.

EDIT__

Here is my vb for the container:

<ParseChildren(True, "Content")> _
Partial Public Class ctrFormContainer
    Inherits System.Web.UI.UserControl

    Private _content As FormControlCollection
    <PersistenceMode(PersistenceMode.InnerDefaultProperty), _
    TemplateInstance(TemplateInstance.Single)> _
    Public Property Content() As FormControlCollection
        Get
            Return _content
        End Get
        Set(ByVal value As FormControlCollection)
            _content = value
        End Set
    End Property

    Protected Overrides Sub CreateChildControls()
        If _content IsNot Nothing Then
            ctrChildren.Controls.Clear()
            For Each i As FormControl In _content
                ctrChildren.Controls.Add(i)
            Next
        End If
        MyBase.CreateChildControls()

    End Sub

    Public Overrides Function FindControl(ByVal id As String) As System.Web.UI.Control
        Return MyBase.FindControl(id)
    End Function

    Public Class FormControlCollection
        Inherits List(Of FormControl)
    End Class
End Class
+1  A: 

Short answer - you can't. The UserControl class inherits from TemplateControl, which implements the INamingContainer interface. What this means is that all user controls are naming containers and in the case of nesting, FindControl will not work.

The solution would be to implement recursive search for a control in the hierarchy, traversing the Controls collection of each item if it doesn't find the control on the topmost level. Here's a sample implementation of this: http://stevesmithblog.com/blog/recursive-findcontrol/

Slavo
thanks, all I know is that asp:PlaceHolder also inherits Control which implements INamingContainer and that does NOT create a new naming container....I wonder how the PlaceHolder Class does it
Bob Fincheimer
PlaceHolder inherits Control, not UserControl or TemplateControl. Control does NOT implement INamingContainer. Open Reflector and see for yourself, it's a big difference.
Slavo
Woah, you're right, I misread it , Usercontrol inherits template control and also implements INamingContainer...I read it wrong, thanks. So a Custom Control instead of a User Control should do it.Thanks
Bob Fincheimer
Exactly. If you don't use User controls all should be fine.
Slavo