0

I'm using the default .NET Membership Provider and filling a Gridview with roles, using Roles.GetAllRoles(). I have a commandfield to delete roles and a boundfield for the role itself. What I need to do is to add the number of users per role, the role column would look like this;

Administrators(4)
Supervisors(12)

To prevent deleting a role that has users, I was thinking of using the RowCreated event to fetch the database and add the value to the roles. Is there a better way to do it?

Edit: Solution To achieve what I need I had to use RowDataBound event.

protected void RoleList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string roleName = ((Label)e.Row.FindControl("RoleNameLabel")).Text;

            if (roleName.Trim().Length > 0 && Roles.RoleExists(roleName))
            {
                Int32 roleCount = Roles.GetUsersInRole(roleName).Count();
                ((Label)e.Row.FindControl("RoleNameLabel")).Text += "(" + roleCount + ")";
            }
        }

    }
4

1 回答 1

0

You can use Roles.GetUsersInRole. For example:

If Roles.GetUsersInRole(strRole).Count > 0 Then
    'do NOT delete this role
End If
于 2013-06-11T14:15:10.620 回答