Can somebody explain to me how to hide a repeater column based on the user privileges.
Say I have:
<asp:Repeater ID="repeater" runat="server>
<HeaderTemplate>
<table id="table_id">
<tr>
<th>Name</th>
<th>Secret Info</th>
<tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>' /></td>
<td><asp:Label ID="Label1" runat="server" Text='<%# Eval("SecretInfo") %>' /></td>
<tr>
<ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td><asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>' /></td>
<td><asp:Label ID="Label1" runat="server" Text='<%# Eval("SecretInfo") %>' /></td>
<tr>
<AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
How would I only display the ‘Secret Info’ column to logged on users?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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>
Method 2
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
Method 3
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>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0