我试图隐藏基于使用以下code用户的角色按钮:
I am trying to hide the button based on the user's role using the following code:
<asp:Button ID="btndisplayrole" Text="Admin Button" Visible='<%= WebApplication1.SiteHelper.IsUserInRole("Admin") %>' runat="server" OnClick="DisplayRoleClick" />
但是当我运行上面的code我收到以下错误信息:
But when I run the above code I get the following error message:
不能从它的字符串重新presentation创建类型的对象'可选System.Boolean';因为'&LT(%)= WebApplication1.SiteHelper.IsUserInRole(管理)%>的可视
Cannot create an object of type 'System.Boolean' from its string representation '<%= WebApplication1.SiteHelper.IsUserInRole("Admin") %>' for the 'Visible'
类的一个有趣的问题。但作为错误消息状态,字符串&LT;%= WebApplication1.SiteHelper.IsUserInRole(管理员)%&GT;
无法转换为布尔
Kind of an interesting issue.. But as the error message states, the string <%= WebApplication1.SiteHelper.IsUserInRole("Admin") %>
cannot be converted to a boolean.
不幸的是我无法解释为什么前pression不评估,而是被视为一个字符串。
Unfortunately i cannot explain why the expression isn't evaluated, but instead is treated like a string.
之所以你的&LT;%#%&GT;
EX pression按预期工作,是因为它被视为大大不同。当页面被编译成类,那么编译器创建类似于这样的事件处理程序:
The reason why your <%# %>
expression works as expected, is because it is treated much differently. When the Page is compiled into a class, then the compiler creates an event handler similar to this:
public void __DataBindingButton2(object sender, EventArgs e)
{
Button button = (Button) sender;
Page bindingContainer = (Page) button.BindingContainer;
button.Visible = HttpContext.Current.User.IsInRole("admin");
}
和挂钩这种方法达到您的控制Control.Databinding事件。正如你所看到的,&LT;%#%&GT;
正在这个时候适当地视为服务器code,而不只是一个随机字符串
and hooks this method up to the Control.Databinding event on your control. As you can see, the <%# %>
is this time properly treated as server code, and not just a random string.
所以我猜的解决方法是使用数据绑定,或去codebehind为AndreasKnudsen建议。
So i guess the solution is either to use databinding, or go to the codebehind as AndreasKnudsen suggests.
下一篇:类中的组件列表组件、类中、列表