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

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -