为什么 User.IsInRole 在这种情况下不起作用?
...我想在用户是管理员时显示“删除”按钮,并在用户是贡献者时显示“添加项目”按钮:
<!-- More code above -->
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton CSSClass="TableRightLink" ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Visible=<%# User.IsInRole(@"DOMAIN\CMDB_ADMIN") %>
Text="Delete"
OnClientClick="return confirm('Are you certain you want to delete this item?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle VerticalAlign="Top" />
<HeaderStyle ForeColor="White" CssClass="TableHeader" BackColor="SteelBlue" />
</asp:GridView>
<asp:table width="100%" runat="server" CSSclass="PromptTable" Visible=<%# User.IsInRole(@"DOMAIN\CMDB_CONTRIBUTE") %> >
<asp:tablerow><asp:tablecell HorizontalAlign=Center>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="AddConfigItem.aspx" ForeColor="LightCyan">Add Item</asp:HyperLink>
</asp:tablecell></asp:tablerow></asp:table>
“删除”按钮“可见”属性工作正常。 但是,“添加项目”超链接却没有。它总是显示。 查看源代码告诉我 %# User.IsInRole(@"DOMAIN\CMDB_CONTRIBUTE") %> 不评估任何东西。 知道这是为什么吗?
...I want to Show the 'delete' button when user is an admin, and show the 'add item' button when user is a contributor:
<!-- More code above -->
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton CSSClass="TableRightLink" ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Visible=<%# User.IsInRole(@"DOMAIN\CMDB_ADMIN") %>
Text="Delete"
OnClientClick="return confirm('Are you certain you want to delete this item?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle VerticalAlign="Top" />
<HeaderStyle ForeColor="White" CssClass="TableHeader" BackColor="SteelBlue" />
</asp:GridView>
<asp:table width="100%" runat="server" CSSclass="PromptTable" Visible=<%# User.IsInRole(@"DOMAIN\CMDB_CONTRIBUTE") %> >
<asp:tablerow><asp:tablecell HorizontalAlign=Center>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="AddConfigItem.aspx" ForeColor="LightCyan">Add Item</asp:HyperLink>
</asp:tablecell></asp:tablerow></asp:table>
The Delete button 'visible' attribute works fine. But, the "add item' hyperlink doesn't. It always shows.
View-source tells me that %# User.IsInRole(@"DOMAIN\CMDB_CONTRIBUTE") %> isn't evaluating to anything. Any idea why this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在 Page_Load 的代码后面设置它,而不是在标记中。 假设id是promptTable(在你的例子中没有给出),只需添加:
大概这需要完成,无论它是否是回发。
FWIW,@Keltex 关于控件未进行数据绑定的说法是正确的,因此
<%# %>
将不起作用。 不幸的是,<%= %>
语法也不会,因为它总是返回一个字符串,而您需要一个布尔值。 我找不到任何其他适用于这种情况的语法。 您可能可以通过使用 javascript 关闭显示来做到这一点,但我怀疑如果不在正确的组中,您不希望将表格呈现到页面(而不是在 DOM 上隐藏或删除)客户)。 我认为在后面的代码中执行此操作是正确的方法。Try setting it in code behind, instead of in mark up, in Page_Load. Assuming the id is promptTable (it wasn't given in your example), just add:
Presumably this needs to be done regardless of whether it is a postback or not.
FWIW, @Keltex is right about the control not being databound so
<%# %>
won't work. Unfortunately, the<%= %>
syntax won't either because it always returns a string and you need a boolean value there. I couldn't find any other syntax that would work in this case. You could probably do this by turing off display using javascript, but I suspect that you don't want the table to be rendered to the page if not in the correct group (as opposed to just being hidden or removed from the DOM once on the client). Doing it in the code behind, I think is the right way to go about it.尝试:
asp:table 似乎没有数据绑定。
Try:
The asp:table doesn't appear to be databound.