Microsoft Visual Studio – ASP.Net How to Make a Hyperlink (or Any Other Control) Visible on Page Based on Role

This article assumes you are using roles and have assigned roles to certain users that log in to your website.

Let’s say I have an aspx page with four hyperlinks. I want HyperLink3 and HyperLink4 to be made visible on the page only if the user logged in is in the “managers” role.

Wrap the following “<% %>” logic around your hyperlinks:

<% If User.IsInRole("managers") Then %>
 <p>
   <asp:HyperLink ID="HyperLink3" runat="server" 
 NavigateUrl="~/HL3.aspx" Target="_blank">Some link description</asp:HyperLink>
 </p>
 <p>
 <asp:HyperLink ID="HyperLink4" runat="server" 
 NavigateUrl="~/HL4.aspx" Target="_blank">Some link description</asp:HyperLink>
 </p>
 <% End If %>

Leave a Reply