asp.net - Convert controls dynamically from strings -


i want disable controls on asp page controlcollection.

this code.

foreach (system.web.ui.control c in controlcollection) {    if (c.gettype().fullname.equals("system.web.ui.webcontrols.table"))    {         tablerow t = (tablerow)c;         t.enabled = false;    }    else if (c.gettype().fullname.equals("system.web.ui.webcontrols.textbox"))    {         textbox t = (textbox)c;         t.enabled = false;    }   .......   ......   ///like controls } 

i need better approach @ this. searched on internet didn't find solution.

do controls in list inherit system.web.ui.webcontrol? if so, code may help. (didn't test myself)

type wc = new system.web.ui.webcontrols.webcontrol(htmltextwritertag.a).gettype();      foreach (system.web.ui.control c in controlcollection)     {         if (c.gettype().issubclassof(wc))         {             ((system.web.ui.webcontrols.webcontrol)c).enabled = false;         }     } 

and more elegant (thanx shadow wizard )

controlcollection.oftype<system.web.ui.webcontrols.webcontrol>().tolist().foreach(c => c.enabled = false); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -