Solved

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

Posted on 2012-03-12
3
216 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
  • 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with C#, MVC, razor. 6 34
Question! 4 36
Adding  DYMO Labelprinter to c# client application 4 36
Graphics 2 30
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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