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 - [Flags]
-
public enum UserRoles
-
{
-
GroupLead = 0x2,
-
WebAdmin = 0x4,
-
Developers = 0x8,
-
Hr = 0x10,
-
Accounts = 0x20,
-
ItInfra= 0x40
-
}
-
-
public static class UserPermission
-
{
-
public static bool IsPorpertySupport(int empId, UserRoles[] uRoles)
-
{
-
// inner logics to return true or false acc. as the user is in the specified roles(s) or not.
-
}
-
}
And from page i'll call it like this - tblAddGrInfo.Visible = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name),new[] {UserRoles.Hr, UserRoles.WebAdmin});
-
But now for many controls (like a textbox, a dropdownlist, a label, a grid etc.) in the page i thought of a function like -
public static bool SetEnable(int empId, System.Web.UI.Control[] ctrlCol, UserRoles uRole)
-
{
-
try
-
{
-
foreach (var c in ctrlCol)
-
c.Enabled = IsPorpertySupport(empId, new [] {uRole}); // ERROR LINE
-
return true;
-
}
-
catch (Exception)
-
{
-
return false;
-
}
-
}
-
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 -
var isUserHr = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name), new[] { UserRoles.Hr });
-
txtEmployeeDOB.Enabled = isUserHr; //TextBox
-
txtEmployeePANNo.Enabled = isUserHr;
-
txtEmployeePassportNo.Enabled = isUserHr;
-
txtEmployeeFirstName.Enabled = isUserHr;
-
txtEmployeeLastName.Enabled = isUserHr;
-
txtEmployeeBankAccNo.Enabled = isUserHr;
-
txtEmployeePFAccNo.Enabled = isUserHr;
-
chkIsEmployeeActive.Enabled = isUserHr; //CheckBox
-
ddlPaysleepDesg.Enabled = isUserHr; //DropDownList
-
ddlWorkingDesg.Enabled = isUserHr; //DropDownList
but if control increases it's going to be difficult...
If I were you I would use Reflection.
First thing you need to do is import the Reflection namespace:
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: -
//retrieving whether or not the control should be enabled
-
bool isUserHr = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name), new[] { UserRoles.Hr });
-
-
//looping through each control
-
foreach (var ctrl in ctrlCol)
-
{
-
//grabbing all of the properties for the control
-
PropertyInfo[] properties = ctrl.GetType().GetProperties();
-
-
//attempting to retrieve the Enabled property for the control
-
PropertyInfo enabledProperty = Array.Find(properties, (pi) => string.Compare(pi.Name, "Enabled", true) == 0);
-
-
//checking whether or not there was an Enabled property
-
if (enabledProperty != null)
-
{
-
//if there is an enabled property, setting the
-
//control's enabled property to the isUserHr
-
//which indicates whether or not the control should
-
//be enabled
-
enabledProperty.SetValue(ctrl, isUserHr, null);
-
}
-
}
-Frinny
3 3178
If I were you I would use Reflection.
First thing you need to do is import the Reflection namespace:
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: -
//retrieving whether or not the control should be enabled
-
bool isUserHr = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name), new[] { UserRoles.Hr });
-
-
//looping through each control
-
foreach (var ctrl in ctrlCol)
-
{
-
//grabbing all of the properties for the control
-
PropertyInfo[] properties = ctrl.GetType().GetProperties();
-
-
//attempting to retrieve the Enabled property for the control
-
PropertyInfo enabledProperty = Array.Find(properties, (pi) => string.Compare(pi.Name, "Enabled", true) == 0);
-
-
//checking whether or not there was an Enabled property
-
if (enabledProperty != null)
-
{
-
//if there is an enabled property, setting the
-
//control's enabled property to the isUserHr
-
//which indicates whether or not the control should
-
//be enabled
-
enabledProperty.SetValue(ctrl, isUserHr, null);
-
}
-
}
-Frinny
Sign in to post your reply or Sign up for a free account.
Similar topics
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 :...
|
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...
|
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
|
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?
|
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;
|
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;
....
|
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...
|
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 }
|
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...
|
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?
...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
| |