views:

376

answers:

1

Hi,

I want to hide a column of ListView based on the role from the code behind. Here's the mark-up and the code:

    <asp:ListView ID="lvTimeSheet" runat="server">
    <LayoutTemplate>
        <table id="TimeSheet">
            <thead>
                <tr>
                    <th id="thDelete" runat="server" Visible='<%# IsAdmin() %>'>
                         Select
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr id="itemPlaceholder" runat="server" />
            </tbody>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:CheckBox ID="cbMarkAsComplete" runat="server" onclick="selectMe(this)" Text=" &nbsp; Delete" />
            </td>
    </ItemTemplate>
</asp:ListView>

In the ListView layout template I have a <th> which has attribute id="thDelete" runat="server" Visible='<%# IsAdmin() %>' . In the code behind,

    Public Function IsAdmin() As Boolean

    If "user is admin" Then
        Return True
    Else
        Return False
    End If

End Function

But that column id="thDelete" is visible all the time. How do I go about hiding the column based on some condition from the code behind? Thank you for any input.

A: 

The tags with the attribute runat="server" can't allow inclusion <% %>. Try this:

    <asp:ListView ID="lvTimeSheet" runat="server">
    <LayoutTemplate>
        <table id="TimeSheet">
            <thead>
<% If IsAdmin() Then %>

                <tr>
                    <th id="thDelete" runat="server">
                         Select
                    </th>
                </tr>
<% End If %>

            </thead>
            <tbody>
                <tr id="itemPlaceholder" runat="server" />
            </tbody>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:CheckBox ID="cbMarkAsComplete" runat="server" onclick="selectMe(this)" Text=" &nbsp; Delete" />
            </td>
    </ItemTemplate>
</asp:ListView>
Fabio