提问人:user2759835 提问时间:8/9/2019 最后编辑:user2759835 更新时间:8/9/2019 访问量:37
无论查找参数如何,RadioButtonList FindControl 始终会找到自身
RadioButtonList FindControl always finds itself regardless of find parameter
问:
我正在遍历控件,以根据某些设置查找要禁用的控件。这是非常旧的 .net 1.1 代码,在我将其升级到 2.0(然后是 4.5)之前运行良好。而且 RadioListButton 只有一个问题。无论查找控件中使用的参数如何,FindControl 方法都会找到自身。请参阅示例代码。请注意,ControlsToDisable 是一个字符串的 ArrayList,其中包含我要禁用的控件的 ID。control 是我发送到此方法的控件。在本例中为 RadioButtonList。您可能想知道为什么我甚至发送 RadioButtonList,因为它通常没有自己的控件。但是我正在递归执行此操作以遍历所有控件以检查应禁用哪些控件。
我尝试使用“真实”代码输入,也只是为 FindControl 输入一个随机字符串,但它总是返回自身而不是什么都不返回。我用什么作为参数并不重要。
If Not Me.ControlsToDisable Is Nothing Then
For count = 0 To Me.ControlsToDisable.Count - 1
If Not control.FindControl(Me.ControlsToDisable.Item(count)) Is Nothing Then
If TypeOf control.FindControl(Me.ControlsToDisable.Item(count)) Is WebControl Then
CType(control.FindControl(Me.ControlsToDisable.Item(count)), WebControl).Enabled = False
ElseIf TypeOf control.FindControl(Me.ControlsToDisable.Item(count)) Is HtmlControls.HtmlControl Then
CType(control.FindControl(Me.ControlsToDisable.Item(count)), HtmlControls.HtmlControl).Disabled = True
ElseIf TypeOf control.FindControl(Me.ControlsToDisable.Item(count)) Is Ajax.Button Then
CType(control.FindControl(Me.ControlsToDisable.Item(count)), Ajax.Button).Enabled = False
ElseIf TypeOf control.FindControl(Me.ControlsToDisable.Item(count)) Is Ajax.DropDownList Then
CType(control.FindControl(Me.ControlsToDisable.Item(count)), Ajax.DropDownList).Enabled = False
End If
End If
Next
End If
我期待这种控制。FindControl(Me.ControlsToDisable.Item(count)) Is Nothing 将返回 true,并且不会命中 if 语句中的代码。但对于 RadioButtonList 控件,这种情况不会发生。而是控制。FindControl(Me.ControlsToDisable.Item(count)) 找到自身。您可以将 Me.ControlsToDisable.Item(count) 替换为“thismakesnosense”,您将获得相同的结果。所有其他控件均按预期工作。.NET 1.1 及更高版本之间的控制发生了哪些变化?
在做了一些测试之后,我添加了一个问题的更简化版本 - RadioButtonList.FindControl(“”) 返回自身,而其他控件(如按钮)返回 Nothing(即 Button.FindControl(“”) 返回 Nothing)。有谁知道为什么该特定控件的反应不同?我现在有一个解决方法(只是不转到我的循环中的最低级别的控件),所以我不需要它来让我的代码工作,但现在我很好奇。它似乎在 2.0 中发生了变化。
答: 暂无答案
上一个:如何从内容页访问母版页控件
下一个:查找控件返回 null
评论
= Nothing
Is Nothing