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
And from page i'll call it like this
But now for many controls (like a textbox, a dropdownlist, a label, a grid etc.) in the page i thought of a function like
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
but if control increases it's going to be difficult...
so i wrote a function like this
Code:
[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. } }
Code:
tblAddGrInfo.Visible = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name),new[] {UserRoles.Hr, UserRoles.WebAdmin});
Code:
public static bool SetEnable(int empId, System.Web.UI.Control[] ctrlCol, UserRoles uRole) { try { foreach (var c in ctrlCol) [I][B][U]c.Enabled = IsPorpertySupport(empId, new [] {uRole});[/U][/B][/I] // ERROR LINE return true; } catch (Exception) { return false; } }
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
Code:
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
Comment