ramrodcar
asked on
Control Iteration : Collection was modified; enumeration operation may not execute.
I'm trying to iterate through a pages controls, and while currently i've been successful setting controls to read only, i'd like to convert them to labels, to make for easy printing. Here is what i have, but when i run it, i get Collection was modified; enumeration operation may not execute.
I know that i'm adding/removing controls so that's why this is happening, but is there a better way to do this then taking the contents of the control, throwing it into a label, removing the original control and then adding the label control to the page?
Public Shared Sub SetFormToReadOnly(ByVal ctrl As System.Web.UI.Control)
Dim txtTemp As TextBox
Dim ddltemp As DropDownList
Dim btnTemp As Button
'iterate through each control in the page, and each controls' controls to get every
'control on the masterpage/content page/ and and associated grouped controls, i.e. panels, etc.
For Each subCtrl As Control In ctrl.Controls
SetFormToReadOnly(subCtrl)
If TypeOf subCtrl Is TextBox Then
txtTemp = subCtrl
txtTemp.ReadOnly = True
Dim tlabel As New Label
tlabel.Text = txtTemp.Text
ctrl.Controls.Remove(txtTe mp)
ctrl.Controls.Add(tlabel)
ElseIf TypeOf subCtrl Is DropDownList Then
ddltemp = subCtrl
ddltemp.Enabled = False
ElseIf TypeOf subCtrl Is Button Then
btnTemp = subCtrl
btnTemp.Enabled = False
End If
Next subCtrl
End Sub
I know that i'm adding/removing controls so that's why this is happening, but is there a better way to do this then taking the contents of the control, throwing it into a label, removing the original control and then adding the label control to the page?
Public Shared Sub SetFormToReadOnly(ByVal ctrl As System.Web.UI.Control)
Dim txtTemp As TextBox
Dim ddltemp As DropDownList
Dim btnTemp As Button
'iterate through each control in the page, and each controls' controls to get every
'control on the masterpage/content page/ and and associated grouped controls, i.e. panels, etc.
For Each subCtrl As Control In ctrl.Controls
SetFormToReadOnly(subCtrl)
If TypeOf subCtrl Is TextBox Then
txtTemp = subCtrl
txtTemp.ReadOnly = True
Dim tlabel As New Label
tlabel.Text = txtTemp.Text
ctrl.Controls.Remove(txtTe
ctrl.Controls.Add(tlabel)
ElseIf TypeOf subCtrl Is DropDownList Then
ddltemp = subCtrl
ddltemp.Enabled = False
ElseIf TypeOf subCtrl Is Button Then
btnTemp = subCtrl
btnTemp.Enabled = False
End If
Next subCtrl
End Sub
I have a page that is divided into panels and then I mark the panel enabled property to false (for certain conditions). The result is, every control inside of that panel is disabled. Its kind of an easy blanket thing that works. The only down side I dont like is that everything is grey, rather than black. I havent played with styles to see if this is changeable. That is a solution that doesnt involve changing textboxes to labels, or vice versa.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Forced accept.
Computer101
EE Admin
Computer101
EE Admin