Solved

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

Posted on 2009-04-14
1
538 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now