ASP.NET Custom Control - Naming Container
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 Strin开发者_开发技巧g) As System.Web.UI.Control
Return MyBase.FindControl(id)
End Function
Public Class FormControlCollection
Inherits List(Of FormControl)
End Class
End Class
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/
精彩评论