c# - Taking action for all controls with an 'ImageUrl' property -


i've got below piece of code, iterates through controls on webpage , imagebutton, modifies imageurl property. i'd like do, go through each control and, if control happens have imageurl property, change. eg normal images etc included in process. feeling generics or might key here, not versed in area least. thoughts? thanks!

        public static void addthemepathtoimages(control parent)     {          foreach (control c in parent.controls)         {             imagebutton = c imagebutton;             if (i != null)             {                 // see if theme specific version of file exists.  if not, point normal images dir.                 if (file.exists(system.web.httpcontext.current.server.mappath("~/app_variants/" + getusertheme().tostring() + "/images/" + i.imageurl)))                 {                     i.imageurl = "~/app_variants/" + getusertheme().tostring() + "/images/" + i.imageurl;                 }                 else                 {                     i.imageurl = "~/images/" + i.imageurl;                 }                }             if (c.hascontrols())             {                 addthemepathtoimages(c);             }         }     } 

you don't need generics, regular inheritance work fine:

since imagebutton inherits imageurl parent class of image, change first bit from:

imagebutton = c imagebutton; 

to:

image = c image; 

then it'll work fine both image , imagebutton

now, if wanted work controls have imageurl property (even custom control doesn't derive image) you'll need use reflection see if object has property of imageurl , if so, use it.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -