這是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