[SOLVED] show html elements based on user's role


I am trying to hide html elements from plain users such that only users.
I have simple asp.net web form setup that makes use of roles provider and membership.

<li runat="server" id="li1" visible ='<%# HttpContext.Current.User.IsInRole("admin") %>'><a href="screeners.aspx"><span> Admin link</span></a></li>

I also am trying this without success:

<li runat="server" id="liAdminUsers" visible ='<%# isAdmin %>'><a  href="screeners.aspx"><span> The Hopper</span></a></li>

the code behind is:

public bool isAdmin =false;
            protected void Page_Load(object sender, EventArgs e)

                isAdmin = Page.User.IsInRole("admin");

update: I know I can get it working by adding in the code behind:

liAdminUsers.Visible = false ;

but want a UI solution in the .aspx code


You are using data-binding syntax <%# ... %>, so you need to call Page.DataBind()

You typically call DataBind() in your Page_Load method.

Answered By – Joe

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *