Using reflection to find property of a control in asp.net 2.0

The following method is used to find control's Enabled property and set the value (true/false) using reflection. You can find any property of the control by specifying property name in GetProperty() method.


public static void SetEnabled(object control, bool flag)
{
try
{
if (control == null)
throw new ArgumentNullException("control", "control is null");
Type itemType = control.GetType();
System.Reflection.PropertyInfo enabledProperty= itemType.GetProperty("Enabled",BindingFlags.Instance | BindingFlags.Public);

if (enabledProperty != null)
{
enabledProperty.SetValue(control, flag, null);
}
else
{
throw new ArgumentException("control does not have an \"Enabled\" property", "control");
}

}
catch (Exception ex)
{
throw ex;
}
}

Comments

Anonymous said…
really helpful method. great work. Thanks

Popular posts from this blog

Fetching Address book AKA Contact information from Device using Xamarin.Forms

Custom Attributes in MVC

Partial view and AJAX Form with MVC3 Razor