如何根据用户角色启用和禁用按钮?
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您必须根据
HttpContext.Current.User.IsInRole("admin")
函数返回值设置Button.Enabled
属性值。要么在 html 中:
要么在代码后面:
You have to set the
Button.Enabled
property value to according to theHttpContext.Current.User.IsInRole("admin")
function returned value.Either in html:
Or in code behind:
在检查角色后的 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));
要么我错过了一些东西,要么解决方案很简单:
Either I'm missing something or the solution is simply:
我假设您使用的是 ASP.NET 按钮控件 - 如果是,则需要将
Visible
和Enabled
按钮属性设置为false
I'm assuming you are using an ASP.NET button control - if you are then you need to set the
Visible
andEnabled
button properties tofalse
这里遇到的主要问题是哈希标记:
<%#
用于标识绑定。除非您在网格视图或表单视图或其他东西中调用它,否则这将不起作用。我建议按照 @Muhammad Akhtar 的建议将其设置在后面的代码中,但是如果您非常想使用 html 端,那么它可能应该是: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: