473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

3 3178
Frinavale
9,735 Expert Mod 8TB
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
sanndeb
33
@Frinavale
Thanks Mate... that was perfect ....
Jun 3 '10 #3
Frinavale
9,735 Expert Mod 8TB
:) Glad I could help :)
Jun 3 '10 #4

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

Similar topics

1
by: maxim vexler | last post by:
in a book i am ready now : O'Reilly - Web Database Application with PHP and MySQL, 2nd ed. by David Lane, Hugh E. Williams on chapter 9 the author give an example for age validation :...
5
by: Suzanne Vogel | last post by:
** Isn't the 'static_cast' operator the same as traditional type casting? ie, Aren't the following ways of getting b1, b2 the same? // 'Derived' is derived from 'Base' Derived* d = new...
1
by: JohnK | last post by:
under the covers is type casting in VB.Net the same as C# ? myObject = CType(..,..) in VB.Net vs myObject = (SomeClass)aObject
1
by: chook | last post by:
Wherein differences between type casting in C++ : static_cast, dinamic_cast, reinterpret_cast, const_cast and C type casting, like xxx = (type)yyy; What, when and why is necessary to use?
9
by: Roman Mashak | last post by:
Hello, All! Given the sample piece of code I have: #include <stdio.h> #include <string.h> int main(void) { short int i, j;
7
by: Wayne M J | last post by:
I have worked out most type casting and the likes but I am curious about one aspect. Endpoint ep...; IPEndPoint iep...; .... ep = (EndPoint)iep; .... iep = (IPEndPoint)ep; ....
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
7
by: Ben R. | last post by:
How does automatic type casting happen in vb.net? I notice that databinder.eval "uses reflectoin" to find out the type it's dealing with. Does vb.net do the same thing behind the scenes when an...
11
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.