Simple ASP.net Role View Control

For a while now I’ve been wishing I didn’t have to do this…
[xml]
<asp :LoginView ID="LoginView1" runat="server">
<rolegroups>
<asp :RoleGroup Roles="Administrator">
<p>Admins only!</p>
</asp>
</rolegroups>
</asp>
[/xml]
…every time I want to have something only an Admin can see. It’s not that it takes long to do, it just seems incredibly inefficient.

So now my first custom control to the rescue, to this point I’ve only used user controls in ASP.net.

[csharp]
using System.Web.Security;
using System.Web.UI.WebControls;

namespace Teknohippy
{
public class AdminView : PlaceHolder
{
public AdminView()
{

}

public override bool Visible
{
get
{
if (DesignMode)
{
return base.Visible;
}
else
{
return base.Visible && Roles.IsUserInRole("Administrator");
}
}
set
{
base.Visible = value;
}
}
}
}

[/csharp]

Now I can just to the following instead…

[xml]
<tekno:AdminRole id="a1" runat="server">
<p>Admins Only!</p>
</tekno:AdminRole>
[/xml]

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>