Nasty bug !
I start on a bad day today, with a nasty bug I can't figure out what's wrong.
I loop through a series of controls using Typeof to populate a form with different values from the database.
The control (D2) is embedded in another control D. This code is from my mainpage where I populate the forms.
PageD2ctl is declared as a Panel, child of D2.
The problem is that when the control is a Radiobutton, and I am sure it is(just checked in debug mode), my code enter correctly in the test, but it's going also in the Checkbox test, like suddenly my Radiobutton is now typed as a Checkbox.
I spent sometime on that, but absolutly no clue about what's going on. I tried everything, transforming my nice ElseIf in If then loop, nothing at all.
The control (D2) is embedded in another control D. This code is from my mainpage where I populate the forms.
PageD2ctl is declared as a Panel, child of D2.
The problem is that when the control is a Radiobutton, and I am sure it is(just checked in debug mode), my code enter correctly in the test, but it's going also in the Checkbox test, like suddenly my Radiobutton is now typed as a Checkbox.
I spent sometime on that, but absolutly no clue about what's going on. I tried everything, transforming my nice ElseIf in If then loop, nothing at all.
If anyone has an idea ?
Dim MyCtl2 As Control
For Each MyCtl2 In PageD2ctl.controls
For Each MyCtl2 In PageD2ctl.controls
If Not (TypeOf MyCtl2 Is LiteralControl) Then
If MyCtl2.ID = dtr("id_q") Then
If TypeOf MyCtl2 Is TextBox Then
Dim ThisTextbox As TextBox = CType(PageD2ctl.findcontrol(dtr("id_q")), TextBox)
ThisTextbox.Text = dtr("answer")
Elseif TypeOf MyCtl2 Is RadioButton Then
Dim ThisRadiobox As RadioButton = CType(PageD2ctl.findcontrol(dtr("id_q")), RadioButton)
If LCase(dtr("answer")) = "true" Then
ThisRadiobox.Checked = True
End If
If MyCtl2.ID = dtr("id_q") Then
If TypeOf MyCtl2 Is TextBox Then
Dim ThisTextbox As TextBox = CType(PageD2ctl.findcontrol(dtr("id_q")), TextBox)
ThisTextbox.Text = dtr("answer")
Elseif TypeOf MyCtl2 Is RadioButton Then
Dim ThisRadiobox As RadioButton = CType(PageD2ctl.findcontrol(dtr("id_q")), RadioButton)
If LCase(dtr("answer")) = "true" Then
ThisRadiobox.Checked = True
End If
'-- and there after succesfully finish the previous test if it's a radiobutton the code enter in the Checkbox one !
ElseIf TypeOf MyCtl2 Is CheckBox Then
Dim ThisCheckbox As CheckBox = CType(PageD2ctl.findcontrol(dtr("id_q")), CheckBox)
If LCase(dtr("answer")) = "true" Then
ThisCheckbox.Checked = True
End If
If LCase(dtr("answer")) = "true" Then
ThisCheckbox.Checked = True
End If
End if
End If
End If
End if
Next
UPDATE:
I found this solution that I share now:
This confirms (for me) that something wrong there with the Controls.
Instead of using an Elseif structure, I write a Select..Case structure like this (Myctl is the ontrol to test):
select case lcase(Myctl.gettype.tostring)
case "system.web.ui.webcontrols.textbox"
.. Mycode
case "system.web.ui.webcontrols.radiobutton"
.. MyCode
case "system.web.ui.webcontrols.checkbox"
.. Mycode
end select
This confirms (for me) that something wrong there with the Controls.
Instead of using an Elseif structure, I write a Select..Case structure like this (Myctl is the ontrol to test):
select case lcase(Myctl.gettype.tostring)
case "system.web.ui.webcontrols.textbox"
.. Mycode
case "system.web.ui.webcontrols.radiobutton"
.. MyCode
case "system.web.ui.webcontrols.checkbox"
.. Mycode
end select