views:

1082

answers:

2

...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?

+1  A: 

Try:

Visible='<%= User.IsInRole(@"DOMAIN\CMDB_CONTRIBUTE") %>'

The asp:table doesn't appear to be databound.

Keltex
Error 1 Cannot create an object of type 'System.Boolean' from its string representation '<%= User.IsInRole(@"DOMAIN\CMDB_CONTRIBUTE") %>' for the 'Visible' property.
Kolten
+2  A: 

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:

promptTable.Visible = User.IsInRole(@"DOMAIN\CMDB_CONTRIBUTE");

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.

tvanfosson