这是VB。NET代码工作对我来说:
<Extension()> _
Function FindChildControlById(ByVal controlToStartWith As Control, ByVal controlIdToFind As String) As Control
If controlToStartWith Is Nothing Then Return Nothing
If controlToStartWith.ID = controlIdToFind Then Return controlToStartWith
For Each childControl As Control In controlToStartWith.Controls
Dim resCtrl As Control = FindChildControlById(childControl, controlIdToFind)
If resCtrl IsNot Nothing Then Return resCtrl
Next childControl
Return Nothing
End Function ' Function FindChildControlById(ByVal controlToStartWith As Control, ByVal controlIdToFind As String) As Control
信用去乔治最初的VB.NET代码。我只修改它少许,有2个功能改变:如果/当null /没有任何东西作为输入控件传递时,我的错误不会发生错误,并且我的作为扩展实现。我的其他3个较小的更改不影响功能,但对我来说,它们是代码简化。但我知道这很主观。
那么这种方法可以用于:
Dim c1 As Control = Page.FindChildControlById("aspControlID")
如果你想将它转换成一个特定的子类控制的,就像这样:
Dim c1 As Control = Page.FindChildControlById("aspControlID")
Dim c As HyperLink = TryCast(c1, HyperLink)
更新:我的功能现在名为'FindChildControlById'(以前是'FindMiControl')。我更喜欢SpeedNet的建议。
微软实施这个的方式是这样的一个笑话,它应该只是工作。哦,你有一个主页?大约500个嵌套母版页如何?该方法应该算出来,并且找到控制周期的必要条件。 – 2013-05-19 01:51:19
你知道你在同一个想法中使用“应该”和“微软”吗? – user1566694 2016-12-13 20:32:28