Search code examples
asp.netloginview

Dynamically display links using the ASP.NET LoginView


I am having problems using a LoginView for what I need. Can somebody tell me if I can do this in a LoginView (and how) or if I need to use code-behind.

I have two roles - Administrator and User. I want to dynamically display links based on the role. I will write out what I want in an if statement because it's easier to explain:

if (role = Administrator) //Display only if administrator.
   Show Hyperlink 1
   Show Hyperlink 2
else
   if (role = User) //Display only if user.
      Show Hyperlink 3
      Show Hyperlink 4
   endif
   //Display these if a user or if non-authenticated user...
   Show Hyperlink 5
   Show Hyperlink 6
   Show Hyperlink 7 
endif

Solution

  • Try something like this it uses the RoleGroups property. You can also specify the RoleGroup for the user, but if you use the LoggedInTemplate it will take affect for all users logged in that don't have a Group in the RoleGroup. Finally, there isn't really a way to have a shared template like the scenario you described with Users/Anonymous, so you may have to duplicate. Another possiblity is that you don't include them in your LoginView and show them to the Administrators as well.

    <asp:LoginView runat="server" ID="LoginView">
        <AnonymousTemplate>
            <asp:HyperLink runat="server" ID="Link5" />
            <asp:HyperLink runat="server" ID="Link6" />
            <asp:HyperLink runat="server" ID="Link7" />
        </AnonymousTemplate>
        <LoggedInTemplate>
            <asp:HyperLink runat="server" ID="Link3" />
            <asp:HyperLink runat="server" ID="Link4" />
            <asp:HyperLink runat="server" ID="Link5" />
            <asp:HyperLink runat="server" ID="Link6" />
            <asp:HyperLink runat="server" ID="Link7" />
        </LoggedInTemplate>
        <RoleGroups>
            <asp:RoleGroup Roles="Administrator">
                <ContentTemplate>
                    <asp:HyperLink runat="server" ID="Link1" />
                    <asp:HyperLink runat="server" ID="Link2" />
                </ContentTemplate>
            </asp:RoleGroup>
        </RoleGroups>
    </asp:LoginView>