p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   asp:RoleGroup: Only pulls first rolegroup in list (http://p2p.wrox.com/showthread.php?t=77738)

kwilliams January 11th, 2010 12:04 PM

asp:RoleGroup: Only pulls first rolegroup in list
 
I have a very simple page that displays a link for each rolegroup that a member belongs to with ASP.NET membership, lets say Group1 and Group2. For some reason, I can only see the first of two links in a list, regardless of which group it is.

I've already verified that both groups are pulling in fine by moving the Group2 link above Group1 to see if it could be viewed: which it was!

If Group1 is listed above Group 2, I see the link to Group1. But if I move the link for Group2 above Group1, I see the Group 2 link. I don't have a bunch of snazzy-dazzy complicated code on this page, so I'm stumped. Why could this be happening? I'd appreciate any help. Here is my code:

groups.aspx.vb
Code:

Partial Class groups
    Inherits System.Web.UI.Page

    'Pull username
    Dim strLoginUserID As String = Context.User.Identity.Name
End Class

groups.aspx
Code:

<%@ Page Language="VB" title="List of Groups" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" EnableSessionState="true" EnableViewState="false" CodeFile="groups.aspx.vb" Inherits="groups" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:LoginView ID="LoginView1" runat="server">
        <RoleGroups>
            <asp:RoleGroup Roles="Group1">
                <ContentTemplate>
                    You are a member of group 1: <asp:HyperLink ID="hlGroup1" NavigateUrl="group1.aspx" runat="server">Group1</asp:HyperLink>
                </ContentTemplate>
            </asp:RoleGroup>
            <asp:RoleGroup Roles="Group2">
                <ContentTemplate>
                    You are a member of group 2: <asp:HyperLink ID="hlGroup2" NavigateUrl="group2.aspx" runat="server">Group2</asp:HyperLink>
                </ContentTemplate>
            </asp:RoleGroup>
        </RoleGroups>
    </asp:LoginView>
</asp:Content>


Imar January 11th, 2010 12:43 PM

Hi there,

The LoginView only displays content for the *first* matched RoleGroup, not for all matching groups:

http://msdn.microsoft.com/en-us/libr...olegroups.aspx
Quote:


The first matching role-group template is displayed to the user. If a user is a member of more than one role, the first role-group template that matches any of the user's roles is used. If more than one template is associated with a single role, only the first defined template will be used.
Cheers,

Imar

kwilliams January 11th, 2010 12:45 PM

Wow, thanks for the quick reply.

That's a real bummer. Do you have any suggestions of alternative ways of doing the same thing?

Imar January 11th, 2010 12:51 PM

You could, if your model allows for it, repeat stuff from Group 1 in Group 2, and place Group2 as the first group. Alternatively, you could use Panel / Placeholder controls and manage the security from Code Behind....

Imar

kwilliams January 11th, 2010 01:03 PM

Those are both good ideas. Inspired by your ideas, I'm thinking of dynamically handling the links on the code-behind (show/hide) depending on their roles using the User.IsInRole method, kinda like this:

If User.IsInRole("Group1") Then
hlGroup1.Visible = True
ElseIf If User.IsInRole("Group2") Then
hlGroup2.Visible = True
End If

Thanks for your help Imar!

Imar January 11th, 2010 01:09 PM

Code:

If User.IsInRole("Group1") Then
  hlGroup1.Visible = True
ElseIf If User.IsInRole("Group2") Then
  hlGroup2.Visible = True
End If

Funny, this is exactly the way the Login View does it. When you're in Group1, hlGroup1 is visible; otherwise hlGroup2 is ;-)

Imar

kwilliams January 11th, 2010 01:32 PM

That is true, but we have an interesting scenario because this page is on our intranet, so every user is technically logged in. So users in the LoggedInTemplate see a message about them not being a member, while users who are members of a group will see links to their respective group.

So that's why I'm needing to go the extra step on this page. Thanks.

Imar January 11th, 2010 04:28 PM

I see, but that should be doable with just the LoginView? I thought you wanted to show Panel1 and Panel2 to users in Group2 in which case you need this code, don't you:

Code:


If User.IsInRole("Group1") Then
  hlGroup1.Visible = True
End If
If User.IsInRole("Group2") Then
  hlGroup2.Visible = True
End If

This way, if you're in both groups you see both panels.

But maybe i misunderstood....

Imar


All times are GMT -4. The time now is 09:34 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.