I finally found my corresponding answer. I donβt want to set the visibility property (and yes itβs easy to guess!) Because it makes my code dirty and itβs not convenient ... Suppose I have a Default page. aspx and there are 3 roles Admin, A, B .... on the Default.aspx page. I want to show content based on user roles, so I use Loginview and its templates as shown below:
<asp:LoginView runat="server" ID="loginviewControl1"> <AnonymousTemplate> <asp:HyperLink runat="server" ID="lnkLogin" Text="Log In" NavigateUrl="~/Account/Login.aspx"></asp:HyperLink> <Anonymous:AnonymousPart ID = "anonym" runat="server" /> </AnonymousTemplate> <LoggedInTemplate> <asp:Label runat="server" ID="WelcomeBackMessage"></asp:Label> </LoggedInTemplate> <RoleGroups> <asp:RoleGroup Roles="Admin"> <ContentTemplate> <Admin:AdminPart ID ="adminContent" runat="server" /> </ContentTemplate> </asp:RoleGroup> <asp:RoleGroup Roles="A"> <ContentTemplate> <RoleA:RoleAPart ID = "RoleAContent" runat="server"/> </ContentTemplate> </asp:RoleGroup> <asp:RoleGroup Roles="B"> <ContentTemplate> <RoleB:RoleBPart ID = "RoleBContent" runat="server" /> </ContentTemplate> </asp:RoleGroup> </RoleGroups> </asp:LoginView>
RoleB: RoleBPart, RoleA: RoleAPart, Admin: AdminPart
are userControls! and here is my server server.
protected void Page_Load(object sender, EventArgs e) { }
I think this is better than setting visibility, because sometimes itβs hard to cope with it !! when a user with a role registered in the only content that is displayed is RoleA: RoleAPart part!
source share