Solved

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

Posted on 2012-03-12
3
220 Views
Last Modified: 2012-03-12
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!
0
Comment
Question by:jessieBoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37710435
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
}
0
 

Author Comment

by:jessieBoo
ID: 37710467
what do I need to reference to use FindControl?
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 37710595
Sorry, just noticed WPF:

use VisualTreeHelper.GetChild instead.

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question