By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,663 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,663 IT Pros & Developers. It's quick & easy.

How to enable/disable controls against a user's permission via Type Casting

P: 33
I want to enable/disable controls of a asp.net page against a logged in user's permission. say 'admin' & 'hr' can change user's birth date text-box in a page but others will see the text-box as disabled.

so i wrote a function like this
Expand|Select|Wrap|Line Numbers
  1. [Flags]
  2.     public enum UserRoles
  3.     {
  4.         GroupLead = 0x2,
  5.         WebAdmin = 0x4,
  6.         Developers = 0x8,
  7.         Hr = 0x10,
  8.         Accounts = 0x20,
  9.         ItInfra= 0x40
  10.     }
  11.  
  12.     public static class UserPermission
  13.     {
  14.         public static bool IsPorpertySupport(int empId, UserRoles[] uRoles)
  15.         {
  16.             // inner logics to return true or false acc. as the user is in the specified roles(s) or not.
  17.         }
  18.     }
And from page i'll call it like this

Expand|Select|Wrap|Line Numbers
  1. tblAddGrInfo.Visible = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name),new[] {UserRoles.Hr, UserRoles.WebAdmin});
  2.  
But now for many controls (like a textbox, a dropdownlist, a label, a grid etc.) in the page i thought of a function like

Expand|Select|Wrap|Line Numbers
  1. public static bool SetEnable(int empId,  System.Web.UI.Control[] ctrlCol, UserRoles uRole)
  2. {
  3.      try
  4.      {
  5.         foreach (var c in ctrlCol)
  6.            c.Enabled = IsPorpertySupport(empId, new [] {uRole}); // ERROR LINE
  7.         return true;
  8.       }
  9.       catch (Exception)
  10.       {
  11.          return false;
  12.       }
  13. }
  14.  
But the problem is a control don't have Enabled property... you have to cast it to its proper type...
My question is how do i do that? Writing cases for each control (textbox, dropdownlist, grid, checkbox, radiobutton etc) is hard to do... Is there a shorter way?

Currently i'm using it in page load like
Expand|Select|Wrap|Line Numbers
  1. var isUserHr = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name), new[] { UserRoles.Hr });
  2. txtEmployeeDOB.Enabled = isUserHr; //TextBox
  3. txtEmployeePANNo.Enabled = isUserHr;
  4. txtEmployeePassportNo.Enabled = isUserHr;
  5. txtEmployeeFirstName.Enabled = isUserHr;
  6. txtEmployeeLastName.Enabled = isUserHr;
  7. txtEmployeeBankAccNo.Enabled = isUserHr;
  8. txtEmployeePFAccNo.Enabled = isUserHr;
  9. chkIsEmployeeActive.Enabled = isUserHr; //CheckBox
  10. ddlPaysleepDesg.Enabled = isUserHr; //DropDownList
  11. ddlWorkingDesg.Enabled = isUserHr; //DropDownList
but if control increases it's going to be difficult...
Jun 2 '10 #1

✓ answered by Frinavale

If I were you I would use Reflection.

First thing you need to do is import the Reflection namespace:
Expand|Select|Wrap|Line Numbers
  1. using System.Reflection;
Then you need to use reflection to retrieve all the properties of the control (Enabled is a property). To retrieve the properties for the control you will use the Control's Type and call the GetProperties() method. This method returns an array of PropertyInfo types. Once you have this array, you need to retrieve the PropertyInfo type that represents the Enabled property (if there is one). If you are able to retrieve the Enabled-PropertyInfo type then you can use it to set the value for your control.

See the comments in the code for a more detailed explanation:
Expand|Select|Wrap|Line Numbers
  1. //retrieving whether or not the control should be enabled
  2. bool isUserHr = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name), new[] { UserRoles.Hr });
  3.  
  4. //looping through each control 
  5. foreach (var ctrl in ctrlCol)
  6. {
  7.   //grabbing all of the properties for the control
  8.   PropertyInfo[] properties = ctrl.GetType().GetProperties();
  9.  
  10.   //attempting to retrieve the Enabled property for the control
  11.   PropertyInfo enabledProperty = Array.Find(properties, (pi) => string.Compare(pi.Name, "Enabled", true) == 0);
  12.  
  13.   //checking whether or not there was an Enabled property
  14.   if (enabledProperty != null)
  15.   {
  16.     //if there is an enabled property, setting the
  17.     //control's enabled property to the isUserHr 
  18.     //which indicates whether or not the control should 
  19.     //be enabled
  20.     enabledProperty.SetValue(ctrl, isUserHr, null);
  21.   }
  22. }
-Frinny

Share this Question
Share on Google+
3 Replies


Frinavale
Expert Mod 5K+
P: 9,731
If I were you I would use Reflection.

First thing you need to do is import the Reflection namespace:
Expand|Select|Wrap|Line Numbers
  1. using System.Reflection;
Then you need to use reflection to retrieve all the properties of the control (Enabled is a property). To retrieve the properties for the control you will use the Control's Type and call the GetProperties() method. This method returns an array of PropertyInfo types. Once you have this array, you need to retrieve the PropertyInfo type that represents the Enabled property (if there is one). If you are able to retrieve the Enabled-PropertyInfo type then you can use it to set the value for your control.

See the comments in the code for a more detailed explanation:
Expand|Select|Wrap|Line Numbers
  1. //retrieving whether or not the control should be enabled
  2. bool isUserHr = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name), new[] { UserRoles.Hr });
  3.  
  4. //looping through each control 
  5. foreach (var ctrl in ctrlCol)
  6. {
  7.   //grabbing all of the properties for the control
  8.   PropertyInfo[] properties = ctrl.GetType().GetProperties();
  9.  
  10.   //attempting to retrieve the Enabled property for the control
  11.   PropertyInfo enabledProperty = Array.Find(properties, (pi) => string.Compare(pi.Name, "Enabled", true) == 0);
  12.  
  13.   //checking whether or not there was an Enabled property
  14.   if (enabledProperty != null)
  15.   {
  16.     //if there is an enabled property, setting the
  17.     //control's enabled property to the isUserHr 
  18.     //which indicates whether or not the control should 
  19.     //be enabled
  20.     enabledProperty.SetValue(ctrl, isUserHr, null);
  21.   }
  22. }
-Frinny
Jun 2 '10 #2

P: 33
@Frinavale
Thanks Mate... that was perfect ....
Jun 3 '10 #3

Frinavale
Expert Mod 5K+
P: 9,731
:) Glad I could help :)
Jun 3 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.