Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to enable and disable button based on user role?

I have a role called 'member' and another 'admin' in Asp.Net website.

I did before, that button should be visible or not and i am successful in that,but,i am not able to get the proper code(aspx.cs) to disable the button so that it may be in view but not at all accessible.

<asp:Button ID="Button4" runat="server" PostBackUrl="~/report.aspx" 
   Text="print in report format" Width="173px" 
   Enabled='<%# HttpContext.Current.User.IsInRole("Admin") %>' /> 

i want that whenever a member login then button "report" should be disabled for him.

like image 679
iti Avatar asked Dec 01 '22 02:12

iti


1 Answers

You have to set the Button.Enabled property value to according to the HttpContext.Current.User.IsInRole("admin") function returned value.

Either in html:

<Button ... Enabled='<%# HttpContext.Current.User.IsInRole("Admin") %>' ... >

Or in code behind:

Button.Enabled = HttpContext.Current.User.IsInRole("Admin");
like image 94
Akram Shahda Avatar answered Dec 04 '22 00:12

Akram Shahda