ASP.NET自定义控件-命名容器 [英] ASP.NET Custom Control - Naming Container

查看:61
本文介绍了ASP.NET自定义控件-命名容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个用户控件,我用作另一个的容器:

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>

mc容器具有一个默认的内部属性,称为content,它是 MyControls 的集合.

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

上面的标记位于 FormView 内部,当我在formview上调用 FindControl 时,它可以找到容器,但是找不到测试.

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 __

当不在 FormView 中时,内部控件的ID确实会显示在设计器页面的一部分中,因此它可以正常工作.

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 __

这是我的容器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 将不起作用.

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.

解决方案是对层次结构中的控件实施递归搜索,如果找不到最顶层的控件,则遍历每个项目的 Controls 集合.这是一个示例实现: http://stevesmithblog.com/blog/recursive-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. Here's a sample implementation of this: http://stevesmithblog.com/blog/recursive-findcontrol/

这篇关于ASP.NET自定义控件-命名容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆