CheckboxList

Posts   
 
    
Rogken
User
Posts: 6
Joined: 27-Jan-2006
# Posted on: 03-Aug-2006 14:36:12   

My apologies in advance if this question has been asked before. I have set of self servicing entities, User and UserInfoRole and Role. You can probably can guess that a userinfo entity has a collection of roles they are a member of. On a asp.net page I have a checkboxlist control that is bound to a role collection. My question is how do I have checkboxes checked off that are the roles that the user is a member of? Thanks in advance. Brett

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Aug-2006 15:27:46   

Either fetch all the roles in a collection, and fetch the roles attached to this specific user in another collection, and then you can find the matches.

You can also fetch those that he is attached to and those which he is not attached to with something like:


SELECT * FROM Role WHERE RoleID NOT IN (SELECT RoleID FROM UserInfoRole WHERE UserID = xx)

And remove the NOT part for the other collection.

This query can be built using FieldCompareSetPredicate, please refer to the manual "Using the generated code -> Adapter/SelfServicing -> Filtering and sorting -> the predicate system"

bdeline
User
Posts: 13
Joined: 13-Jul-2006
# Posted on: 10-Aug-2006 02:11:59   

Hi Walaa, The avenue I took was the following:

  1. Bind all my roles to checkboxlist
  2. In my OnDataBound function for the checkboxlist I retrieved my Users roles and looped over the items in my checkboxlist. For each item I looked for the role in my users rolecollection.

Heres some code because my explanation is awful


protected void chkList_OnDataBound(object sender, EventArgs e)
    {
        UserInfoEntity uie = new UserInfoEntity(new Guid(GridView1.SelectedDataKey.Value.ToString()));
        RoleCollection rc = uie.GetMultiRoleCollectionViaUserInfoRole(true);
        foreach (ListItem li in ((CheckBoxList)sender).Items)
        {
            IPredicateExpression filter = new PredicateExpression(RoleFields.RoleGuid == new Guid(li.Value));
            
            if (rc.FindMatches(filter).Count > 0)
            {
                li.Selected = true;
            }
        }
    }

I hope this helps anyone looking to do something similar. Brett