如何根据用户角色启用和禁用按钮?

发布于 2024-11-07 17:00:49 字数 384 浏览 4 评论 0原文

我在 Asp.Net 网站中有一个名为“成员”的角色和另一个“管理员”。

我之前做过,该按钮应该可见或不可见,我在这方面取得了成功,但是,我无法获得正确的代码(aspx.cs)来禁用该按钮,以便它可以在视图中,但根本无法访问。

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

我希望每当会员登录时,就应该为他禁用“报告”按钮。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

假装不在乎 2024-11-14 17:00:49

您必须根据 HttpContext.Current.User.IsInRole("admin") 函数返回值设置 Button.Enabled 属性值。

要么在 html 中:

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

要么在代码后面:

Button.Enabled = HttpContext.Current.User.IsInRole("Admin");

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");
妥活 2024-11-14 17:00:49
if (HttpContext.Current.User.IsInRole("member"))
{
  //enable/disable here
}
if (HttpContext.Current.User.IsInRole("member"))
{
  //enable/disable here
}
内心旳酸楚 2024-11-14 17:00:49

在检查角色后的 Page_Load 中,您可以将按钮的 IsEnabled 设置为 False。

例如,buttonLogin.Enabled = (IsUserInRole(Admin));

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));

风流物 2024-11-14 17:00:49

要么我错过了一些东西,要么解决方案很简单:

button.Enabled = false;

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

button.Enabled = false;
成熟稳重的好男人 2024-11-14 17:00:49

我假设您使用的是 ASP.NET 按钮控件 - 如果是,则需要将 VisibleEnabled 按钮属性设置为 false

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

心如狂蝶 2024-11-14 17:00:49

这里遇到的主要问题是哈希标记:<%# 用于标识绑定。除非您在网格视图或表单视图或其他东西中调用它,否则这将不起作用。我建议按照 @Muhammad Akhtar 的建议将其设置在后面的代码中,但是如果您非常想使用 html 端,那么它可能应该是:

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

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() %>'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文