![](/img/trans.png)
[英]ASP.NET DataSource Control “does not have a naming container” exception
[英]ASP.NET Custom Control - Naming Container
我有2個用戶控件,我用作另一個的容器:
<mc:Container runat="server" ID="container">
<mc:MyControl runat="server" ID="test">
</mc:Container>
mc容器有一個默認的內部屬性,稱為content,它是MyControls
的集合。
上面的標記位於FormView
,當我在FormView
調用FindControl
時,它可以找到容器,但找不到測試。
如何使容器控件不創建新的命名容器?
編輯__
當不在FormView
,內部控件的ID確實會在設計器中顯示為頁面的一部分,因此可以正常工作。
編輯__
這是我的容器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
簡短答案-您不能。 UserControl類繼承自TemplateControl ,后者實現了INamingContainer接口。 這意味着所有用戶控件都在命名容器,並且在嵌套的情況下, FindControl將不起作用。
解決方案是對層次結構中的控件實施遞歸搜索,如果找不到最頂層的控件 ,則遍歷每個項目的Controls集合。 這是此示例的實現: http : //stevesmithblog.com/blog/recursive-findcontrol/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.