【发布时间】:2011-12-12 20:58:33
【问题描述】:
我需要浏览页面上的所有控件,寻找特定的控件。我们有一堆用户控件、母版页、内容面板等等。
基本思想很简单,但是一旦我找到我想要的控件,比如五个“层”,控件只返回一层。
我知道我可以做一些俗气的事情,比如拥有一个私有变量并将控制权分配给兔子洞中的那个,但我认为必须有更正式的方法来做到这一点。
还有,这就是所谓的尾递归吗?
我们使用的是 3.5 框架。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim c As Control = getNamedControl(Page, "tb")
End Sub
Private Function getNamedControl(ByVal root As Control, ByVal theTarget As String) As Control
If root.Controls.Count < 1 Then
If Not IsNothing(root.ID) Then
If root.ID = theTarget Then
Return root
End If
End If
Else
For Each c As Control In root.Controls
If c.ID = theTarget Then
Return c
Else
getNamedControl(c, theTarget)
End If
Next
End If
End Function
【问题讨论】:
标签: vb.net recursion asp.net-3.5