2013-06-11 88 views
0

我正在使用默認的.NET成員資格提供程序,並使用Roles.GetAllRoles()填充具有角色的Gridview。我有一個命令字段來爲角色本身刪除角色和綁定字段。我需要做的是添加每個角色的用戶數量,角色列將如下所示;獲取每個角色的用戶數

管理員(4)
監事(12)

爲了防止刪除該用戶擁有一個角色,我想用RowCreated事件來獲取數據庫和值添加到角色中。有沒有更好的方法來做到這一點?

編輯:解決方案 爲了實現我所需要的,我必須使用RowDataBound事件。

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 + ")"; 
      } 
     } 

    } 

回答

0

您可以使用Roles.GetUsersInRole。例如:

If Roles.GetUsersInRole(strRole).Count > 0 Then 
    'do NOT delete this role 
End If 
+0

謝謝tezzo,讓我的一天:) – Mario