[英]ASP.NET Custom Control - Naming Container
I have 2 user controls, I is used as a container for the other: 我有2个用户控件,我用作另一个的容器:
<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
. mc容器有一个默认的内部属性,称为content,它是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. 上面的标记位于FormView
,当我在FormView
调用FindControl
时,它可以找到容器,但找不到测试。
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. 当不在FormView
,内部控件的ID确实会在设计器中显示为页面的一部分,因此可以正常工作。
EDIT__ 编辑__
Here is my vb for the container: 这是我的容器vb:
<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
Short answer - you can't. 简短答案-您不能。 The UserControl class inherits from TemplateControl , which implements the INamingContainer interface. UserControl类继承自TemplateControl ,后者实现了INamingContainer接口。 What this means is that all user controls are naming containers and in the case of nesting, FindControl will not work. 这意味着所有用户控件都在命名容器,并且在嵌套的情况下, FindControl将不起作用。
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. 解决方案是对层次结构中的控件实施递归搜索,如果找不到最顶层的控件 ,则遍历每个项目的Controls集合。 Here's a sample implementation of this: http://stevesmithblog.com/blog/recursive-findcontrol/ 这是此示例的实现: http : //stevesmithblog.com/blog/recursive-findcontrol/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.