Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VB.net中将特定WebControl类型作为参数传递_Vb.net_Parameters_Types_Web Controls - Fatal编程技术网

在VB.net中将特定WebControl类型作为参数传递

在VB.net中将特定WebControl类型作为参数传递,vb.net,parameters,types,web-controls,Vb.net,Parameters,Types,Web Controls,我正在尝试创建一个函数,用于搜索WebControl的父子关系(基本上与WebControl.FindControl(id为字符串)相反,但查找特定的WebControl类型) 例如: 我在GridViewRow的ItemTemplate中有一个用户控件。我正在尝试从用户控件引用GridViewRow。用户控件可能在div或其他类型的控件中,也可能不在其中,因此我不知道要查看多少父控件(即,我不能只使用userControl.parent.parent)。我需要一个函数来查找它在父子层次结构上找

我正在尝试创建一个函数,用于搜索WebControl的父子关系(基本上与WebControl.FindControl(id为字符串)相反,但查找特定的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”不起作用——这就是我要找的

我相信有更好的方法可以做到这一点,所以请随意指出,让我看起来简单


谢谢大家

您应该能够使用递归轻松地做到这一点。这里有一个例子,让你开始或可能解决你的问题

VB语法不再那么好了,但我相信你可以通过一个转换器(比如converter.telerik.com)来运行它

C#代码
谢谢真是太棒了!另外,谢谢你提醒我使用递归。自从我停止参加CS课程以来,我肯定还没有足够地使用它。很高兴听到这个消息。大约一年前,我也有同样的需求,但找不到我的原始代码,所以我把它拼凑起来,很高兴它能工作。
public T FindParentControl<T>(
    ref WebControl child, 
    int currentLevel, 
    int maxLevels)
    where T : WebControl
{
    if (child.Parent == null || currentLevel > maxLevels)
        return null;

    if (child.Parent is T)
        return child.Parent as T;
    else 
        return FindParentControl<T>(
            child.Parent, 
            currentLevel + 1, 
            maxLevels);
}
Public Function FindParentControl(Of T As WebControl)(
    ByRef child As WebControl,
    currentLevel As Integer, 
    maxLevels As Integer) As T

    If child.Parent = Nothing OrElse currentLevel > maxLevels Then
        Return Nothing
    End If

    If TypeOf child.Parent Is T Then
        Return TryCast(child.Parent, T)
    Else
        Return FindParentControl(Of T)(child.Parent, currentLevel + 1, maxLevels)
    End If
End Function