Solved

how to use Roles.GetUsersInRole by selecting roles from checkbox list

Posted on 2009-04-14
1
552 Views
Last Modified: 2012-05-06
I have app using standard roles and membership.  I have checkbox that is populated with all roles like this

           checkbox1.DataSource = Roles.GetAllRoles();
           checkbox1.DataBind();

I want to check a number of roles on the checkbox and if the role is selected then add all usernames to a list.  Something like

foreach (ListItem theseRoles in checkbox.Items)
            {
                if (checkbox.Selected)

//then add all the usernames to the list

listofUserNames = new List<string>(Roles.GetUsersInRole(checkbox.text));

//and so on adding all the usernames to the list for each checkbox role checked.

How can I implement this?
0
Comment
Question by:mugsey
1 Comment
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 24144643
Hi mugsey,
>>.. I want to check a number of roles on the checkbox and if the role is selected then add all usernames to a list.
Try this:

Further details:
http://www.4guysfromrolla.com/demos/printPage.aspx?path=/articles/052307-1.aspx

             int i;
            // Get all of the users
             MembershipUserCollection allUsers = Membership.GetAllUsers();
             MembershipUserCollection filteredUsers = new MembershipUserCollection();
 
             for (i = 0; i < checkbox1.Items.Count; i++)
             {
                 if (checkbox1.Items[i].Selected)
                 {
                     // If we are filtering by role, get the users in the specified role
                     string[] usersInRole = Roles.GetUsersInRole(checkbox1.Items[i].Value);
   
                     // For each user in the role, add the user details to filteredUsers
                     foreach (MembershipUser user in allUsers)
                     {
                        foreach (string userInRole in usersInRole)
                         {
                            if (userInRole == user.UserName)
                            {
                              filteredUsers.Add(user);
                              break; // Breaks out of the inner foreach loop to avoid unneeded checking.
                            }
                         }
                     }
                 }
             }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question