Solved

Controls based on roles or logged in

Posted on 2007-11-29
8
565 Views
Last Modified: 2012-05-05
How do I wrap code around a user control in order to indicate that it should only be displayed if the user has a certain role?

Same also for only displaying if a user is logged in?

I know how to register one and put it on the page, I just don't know how to do the if/else stuff.

thanks!


0
Comment
Question by:Starr Duskk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 20375838
Two options imo.
1) use the provided control (asp:LoginView) and use the appropriate template
2) use a little bit of code (<asp:.... visible='<%# Page.User.IsAuthenticated%>' /> or <asp:.... visible='<%# Page.User.IsInRole("administrator")%>' />)


0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20376752
when I use that on a control:
Visible="<%# Page.User.IsInRole("administrator")%>"

I get an error: Parser Error Message: The server tag is not well formed.

<uc:RBox ID="RBox1" runat="server" Visible="<%# Page.User.IsInRole("administrator")%>" />

Isn't there a way to do multiple roles?

Or if the user is NOT in the role?

0
 
LVL 22

Accepted Solution

by:
CJ_S earned 500 total points
ID: 20381122
Well - you didn't copy the code as I gave you. Note the different quotation marks

Visible='<%# Page.User.IsInRole("administrator")%>'

Multiple roles:
Visible='<%# (Page.User.IsInRole("administrator") or Page.User.IsInRole("editor"))%>'

Not in a role (using C#)
Visible='<%# !Page.User.IsInRole("administrator")%>'

Not in a role (using VB.Net)
Visible='<%# Not Page.User.IsInRole("administrator")%>'


0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 2

Author Closing Comment

by:Starr Duskk
ID: 31411718
great answer!
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20402830
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20403075
Ugh. I graded too soon.

this:
<uc:BannerAdvfn ID="Banner1" runat="server" visible='<%# Page.User.IsAuthenticated%>'  />

gives this error:
Compiler Error Message: BC30456: 'IsAuthenticated' is not a member of  'System.Security.Principal.IPrincipal'.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 20409974
My bad

'<%# Page.User.Identity.IsAuthenticated%>'
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20413756
Well, even that doesn't seem to work with single or double quotes (it says I have to have double to match the rest of my control), or NOT or not NOT. It always displays whether logged in or not:

<uc:MenuAd ID="Menu2" runat="server" Visible="<%# Page.User.Identity.IsAuthenticated%>"/>

But that's okay. Someone else showed me how to define it in the page load... which works better for my purposes anyway.

thanks.


        If isLoggedIn Then ' defined above.....
            Me.Menu1.Visible = False
        Else
            Me.Menu2.Visible = False
        End If
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question