Solved

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

Posted on 2012-03-12
3
217 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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