Hide Repeater columns based on user privileges

前端 未结 3 1704

Can somebody explain to me how to hide a repeater column based on the user privileges.

Say I have:



        
相关标签:
3条回答
  • 2021-01-19 10:55

    You can render the <td> elements conditionally. This simplified example presumes you have a Page-level property that indicates whether or not the user is logged on (you'll want to do the same thing in the header template):

    <asp:Repeater ID="repeater" runat="server>
        <ItemTemplate>
            <tr>
                <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'  /></td>
                <% if (this.UserIsLoggedOn) { %>
                <td><asp:Label ID="Label2" runat="server" Text='<%# Eval("SecretInfo") %>' /></td>
                <% } %>
            <tr>
        </ItemTemplate>
    </asp:Repeater>
    
    0 讨论(0)
  • 2021-01-19 10:58

    You could do something like the following:

    <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("SecretInfo") %>' Visible='<%# IsUserLoggedOn() %>' /></td>
    

    Where IsUserLoggedOn is some function that returns a boolean that is true if the user is logged on

    0 讨论(0)
  • 2021-01-19 11:01

    You can use the loginview control which can display controls based on role (including anonymous vs authenticated). You can find a guide here: http://weblogs.asp.net/sukumarraju/archive/2010/07/28/role-based-authorization-using-loginview-control.aspx

    So something like:

    <asp:Repeater ID="repeater" runat="server>
      <HeaderTemplate>
        <table id="table_id">
          <tr>
            <th>Name</th>
            <asp:LoginView ID="lvwHeader" runat="server">
              <LoggedInTemplate>
                <th>Secret Info</th>
              </LoggedInTemplate>
            </asp:LoginView>
          <tr>
      </HeaderTemplate>
      <ItemTemplate>
          <tr>
            <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>' /></td>
            <asp:LoginView ID="lvwItem" runat="server">
              <LoggedInTemplate>
                <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("SecretInfo") %>' /></td>
              </LoggedInTemplate>
            </asp:LoginView>
          <tr>
      <ItemTemplate>
      <AlternatingItemTemplate>
          <tr>
            <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>' /></td>
            <asp:LoginView ID="lvwItem" runat="server">
              <LoggedInTemplate>
                <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("SecretInfo") %>' /></td>
              </LoggedInTemplate>
            </asp:LoginView>
          <tr>
      <AlternatingItemTemplate>
      <FooterTemplate>
        </table>
      </FooterTemplate>
    </asp:Repeater>
    
    0 讨论(0)
提交回复
热议问题