How to check if control is enabled with control name stored in database
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!
C#
Last Comment
Kyle Abrahams
8/22/2022 - Mon
Kyle Abrahams
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
}
Note that Findcontrol only searches it's direct children. If you need to, you can recurse by doing the following:
Private Control FindControlRecurrsive(Cont
{
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
}
return null;
}
otherwise just:
Control ctrl = MyForm.FindControl(myContr
if (ctrl != null && ctrl.IsEnabled)
{
//do something
}