Reputation: 67
I am iterating through controls to find controls to disable based on some settings. This is very old .net 1.1 code which worked fine until I upgraded it to 2.0 (and then 4.5). And there is only an issue with the RadioListButton. The FindControl method finds itself regardless of the parameter used in find control. See sample code. Note that ControlsToDisable is an ArrayList of strings containing ids of controls that I want to disable. control is a control I send to this method. In this case a RadioButtonList. You might wonder why I even send a RadioButtonList since it doesn't typically have controls of its own. But I am doing this recursively to iterate through all the controls to check which ones should be disabled.
I have tried putting in using the 'real' code and also just putting in a random string for FindControl, but it always returns itself instead of nothing. Doesn't matter what I use as the parameter.
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
I would expect that control.FindControl(Me.ControlsToDisable.Item(count)) Is Nothing would return true and it wouldn't hit the code inside the if statement. But this is not happening for RadioButtonList controls. Instead control.FindControl(Me.ControlsToDisable.Item(count)) finds itself. You could replace Me.ControlsToDisable.Item(count) with "thismakesnosense" and you get the same results. All other controls work as expected. What changed with that control between .NET 1.1 and higher versions?
After doing some testing, I'm adding a more simplified version of the question - RadioButtonList.FindControl("") returns itself whereas other controls like button return Nothing (i.e. Button.FindControl("") returns Nothing). Anyone know the why that particular control reacts differently? I now have a workaround (just not going to the lowest level of controls in my loop) so I don't need it for my code to work but now I'm curious. It seems to have changed in 2.0.
Upvotes: 1
Views: 38