c# - Dynamically Removing Controls from Update Panel. -


i have update panel , need remove controls dynamically added can re-add them on asyncpostback. everytime try error stating "collection modified; enumeration operation may not execute". below code using try remove literalcontrols, however; once figure out issue have remove other types. have any

                    foreach(control xxl in updatepanel1.contenttemplatecontainer.controls.oftype<control>())                 {                     label2.text = label2.text + xxl.gettype();                      foreach (literalcontrol lc in updatepanel1.contenttemplatecontainer.controls.oftype<literalcontrol>())                     {                     literalcontrol mylit = (literalcontrol)lc;                     updatepanel1.contenttemplatecontainer.controls.remove(mylit);                     updatepanel1.update();                     }                 } 

you cannot modify items using foreach loop, dynamic control or not.

if want modify items, have use normal for loop.

some general advice using dynamic controls hard , advised avoid @ costs, , use different asp:panels change content toggling visibility. updatepanels tend of finicky controls , can place inside them limited. understand "general" advice may hardly applicable particular project, best can.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -