Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Linq" %>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Page.IsPostBack)
{
// Bind drop down list of users
var users = Membership.GetAllUsers();
UserList.DataSource = users;
UserList.DataTextField = "UserName";
UserList.DataValueField = "UserName";
UserList.DataBind();
// Bind roles for current user
var currentUserRoles = Roles.GetRolesForUser()
.OrderBy(x => x);
RolesCurrentUser.DataSource = currentUserRoles;
RolesCurrentUser.DataBind();
}
if (Page.IsPostBack)
{
// Bind roles for selected user from drop down list
if (UserList.SelectedValue != string.Empty)
{
var roles =
Roles.GetRolesForUser(UserList.SelectedValue)
.OrderBy(x => x);
RolesRepeater.DataSource = roles;
RolesRepeater.DataBind();
}
}
}
</script>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<fieldset>
<legend>Users and roles in system</legend>
<asp:DropDownList ID="UserList" runat="server" AutoPostBack="true" />
<br />
<asp:Repeater ID="RolesRepeater" runat="server">
<HeaderTemplate>
<h2>Roles for <%= UserList.SelectedValue %></h2>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<%# Container.DataItem %>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</fieldset>
<fieldset>
<legend>My roles</legend>
<asp:Repeater ID="RolesCurrentUser" runat="server">
<HeaderTemplate>
<h2>Roles for <%= User.Identity.Name %> (current user)</h2>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<%# Container.DataItem %>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</fieldset>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment