4

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.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
iti
  • 203
  • 3
  • 5
  • 16

6 Answers6

6
if (HttpContext.Current.User.IsInRole("member"))
{
  //enable/disable here
}
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • +1: This should really be done in the code behind. It can be done in the html side, but there's really no point to do it that way. It's even less readable/maintainable IMO. – Joel Etherton May 13 '11 at 15:07
6

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");
Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
  • thanks it's done.I have no words to explain how much this site boosts my confidence.Thanks once again – iti May 16 '11 at 05:36
3

In the Page_Load after checking for the role you may be able to set the IsEnabled for the Button to be False.

e.g. buttonLogin.Enabled = (IsUserInRole(Admin));

kanchirk
  • 912
  • 2
  • 13
  • 29
2

Either I'm missing something or the solution is simply:

button.Enabled = false;
SirViver
  • 2,411
  • 15
  • 14
1

I'm assuming you are using an ASP.NET button control - if you are then you need to set the Visible and Enabled button properties to false

jcvandan
  • 14,124
  • 18
  • 66
  • 103
1

The primary problem you have here is the hash mark: <%# is used to identify a binding. Unless you're calling this in a gridview or a formview or something, this will not work. I would recommend setting it in the code behind as suggested by @Muhammad Akhtar, but if you're hell bent for leather on using the html side it should probably be:

Enabled='<%= HttpContext.Current.User.IsInRole("Admin").ToString() %>'
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104