【发布时间】:2009-04-23 22:15:27
【问题描述】:
我正在尝试创建一个函数来搜索 WebControl 的父子关系(基本上与 WebControl.FindControl(id as String) 相反,但要查找特定的 WebControl 类型)。
示例: 我在 GridViewRow 的 ItemTemplate 中有一个用户控件。我正在尝试从用户控件中引用 GridViewRow。用户控件可能在也可能不在 div 或其他类型的控件中,所以我不知道到底要查看多少父级(即我不能只使用 userControl.Parent.Parent)。我需要一个函数来找到它在父子层次结构中找到的第一个 GridViewRow。
因此需要这个功能。除非有更好的方法来做到这一点? 无论如何,我希望我正在创建的函数相当通用,以便可以根据我正在寻找的内容指定不同的 WebControl 类型(即 GridViewRow、Panel 等)。这是我写的代码:
Public Function FindParentControlByType(ByRef childControl As WebControl, ByVal parentControlType As WebControl.Type, Optional ByRef levelsUp As Integer = Nothing) As WebControl
Dim parentControl As WebControl = childControl
Dim levelCount = 1
Do While Not parentControl.GetType = parentControlType
If Not levelsUp = Nothing AndAlso levelCount = levelsUp Then
parentControl = Nothing
Exit Do
End If
levelCount += 1
parentControl = parentControl.Parent
Loop
parentControl.FindControl(
Return parentControl
End Function
我知道函数定义中的“ByVal parentControlType as WebControl.Type”不起作用——这就是我要找的。p>
我确信有更好的方法可以做到这一点,所以请随时指出来让我看起来很简单!
谢谢各位!
【问题讨论】:
标签: vb.net parameters types web-controls