We help IT Professionals succeed at work.

How to check if control is enabled with control name stored in database

jessieBoo
jessieBoo asked
on
I have a control name stored in a database table. I would like to pass the control name I retrieve from the database and check whether the control is enabled. How do I pass the string which has the control name to the c# code and check if enabled.

If I knew the control name ahead of time I would simply do:

if (MyControl.IsEnabled)  
{
                //////
}

But if I had:

string MycontrolName = "SomeName";

How do I construct the check for the enable?

Thanks!
Comment
Watch Question

Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
Use FindControl.

Note that Findcontrol only searches it's direct children.  If you need to, you can recurse by doing the following:

Private Control FindControlRecurrsive(Control parent, String name)
{

   if (parent.Name.tolower() == name.tolower()) // I always ignore case when comparing names.
      return parent;
   else

  for each ctrl in Parent.Controls
   {
    if (ctrl.Name == name)
      return ctrl;
     else if (ctrl.Controls.Count > 0)
           return   FindControlRecurrsive(ctrl, name);
   }

   return null;
}

otherwise just:


Control ctrl = MyForm.FindControl(myControlName);

if (ctrl != null && ctrl.IsEnabled)
{
 //do something
}

Author

Commented:
what do I need to reference to use FindControl?
Senior .Net Developer
CERTIFIED EXPERT
Commented:
Sorry, just noticed WPF:

use VisualTreeHelper.GetChild instead.

http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls

Explore More ContentExplore courses, solutions, and other research materials related to this topic.