Solved

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

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

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 39

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now