Updating User Roles

Last post 05-18-2008 3:24 PM by amensi. 7 replies.

Sort Posts:

  • Updating User Roles

    05-12-2008, 2:36 PM
    • Loading...
    • Icarus1_uk
    • Joined on 04-19-2008, 5:06 PM
    • Posts 38

    Hi Guys, 

     Have written a Sub to update my user roles. The gvMembers refers to the Username taken from a a gridview used to select the user, cblRoles is a CheckBoxList which show all available roles and rolebox refers to the ListItems which create the checkboxes. However for some reason it does not recognise the checkboxes as being checked. Tried putting a breakpoint in the Else part of the code and every checked box hit it including the ones I had checked. Code follows:

    Private Sub UpdateUserRoles()

            For Each rolebox As ListItem In cblRoles.Items
                If rolebox.Selected Then
                    If Roles.IsUserInRole(gvMembers.SelectedValue, rolebox.Text) = False Then
                        Roles.AddUsersToRole(gvMembers.SelectedValue, rolebox.Text)
                    End If
                Else
                    If Roles.IsUserInRole(gvMembers.SelectedValue, rolebox.Text) Then
                        Roles.RemoveUserFromRole(gvMembers.SelectedValue, rolebox.Text)
                    End If
                End If
            Next

        End Sub

     Any ideas why it's not working?
     

  • Re: Updating User Roles

    05-12-2008, 2:53 PM
    • Loading...
    • amensi
    • Joined on 02-13-2006, 2:43 PM
    • Canada
    • Posts 363

    Hi,

    Post your HTML please and check is your checkbox has the property AutoPostBack set to true.

    Alex Mensi - Check out my blog
    Maven Technologies Inc.
    --

    Don't forget to click "Mark as Answer" on the post that helped you.
  • Re: Updating User Roles

    05-12-2008, 5:07 PM
    • Loading...
    • Icarus1_uk
    • Joined on 04-19-2008, 5:06 PM
    • Posts 38

     HTML is simply:


                            <asp:CheckBoxList ID="cblRoles" runat="server">
                            </asp:CheckBoxList>

    The checkboxlist is populated by: 

       cblRoles.DataSource = Roles.GetAllRoles()
            cblRoles.DataBind()

                For Each checkbox As ListItem In cblRoles.Items
                    checkbox.Enabled = False
            Next


            Dim userRoles As String() = Roles.GetRolesForUser(gvMembers.SelectedValue)
            For Each role As String In userRoles
                Dim checkbox As ListItem = cblRoles.Items.FindByValue(role)
                checkbox.Selected = True
            Next
     

  • Re: Updating User Roles

    05-14-2008, 3:00 PM

    Here is my idea to solve your problem

    first thing you will get role that user assigned to him and second check if user role match what he select show him that he is already in that role

    but if user not assigned to selected role from CheckBoxList you will delete user from role and add him to role that he selected from CheckBoxList

    Hope that i could help you

     

    Regards,
    Yasser Zaid

    ~ Please remember to click Mark as Answer on this post if it helped you ~
  • Re: Updating User Roles

    05-14-2008, 3:49 PM
    • Loading...
    • amensi
    • Joined on 02-13-2006, 2:43 PM
    • Canada
    • Posts 363

    Unless you set your CheckBox list like this:  

    <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True">
        </asp:CheckBoxList>
     Your codebehind will never be able to detect what checkbox is checked.
    Alex Mensi - Check out my blog
    Maven Technologies Inc.
    --

    Don't forget to click "Mark as Answer" on the post that helped you.
  • Re: Updating User Roles

    05-15-2008, 4:24 AM
    • Loading...
    • Icarus1_uk
    • Joined on 04-19-2008, 5:06 PM
    • Posts 38

     Autopost back doesn't work. The page just post back and clears the check box again.

  • Re: Updating User Roles

    05-15-2008, 5:23 AM
    • Loading...
    • Icarus1_uk
    • Joined on 04-19-2008, 5:06 PM
    • Posts 38

     Have tried giving each role their own individual check box rather than a check box list and given each one the following code (obviously with different control names).

            If ChkAdmin.Checked = True Then
                If Roles.IsUserInRole(gvMembers.SelectedValue, ChkAdmin.Text) = False Then
                    Roles.AddUsersToRole(gvMembers.SelectedValue, ChkAdmin.Text)
                End If
            Else
                If Roles.IsUserInRole(gvMembers.SelectedValue, ChkAdmin.Text) Then
                    Roles.RemoveUserFromRole(gvMembers.SelectedValue, ChkAdmin.Text)
                End If
            End If

    Now when I click update I get the following error in the error textBox on my page rather than the dreaded ASP.NET error page: 

    Update Failed: Unable to cast object of type 'System.String' to type 'System.String[]'. 

    Any ideas? If anyone can figure the problem with my first way doing it that would be even better. 

  • Re: Updating User Roles

    05-18-2008, 3:24 PM
    • Loading...
    • amensi
    • Joined on 02-13-2006, 2:43 PM
    • Canada
    • Posts 363

    Do you put your code to load the checkboxes in the page_load event? If so are you wrapping your code in: 

    If not Page.IsPostBack then
    
    ...Code here
    
    End If
     If not it would explain why your checkboxes are reset after the postback.
    Alex Mensi - Check out my blog
    Maven Technologies Inc.
    --

    Don't forget to click "Mark as Answer" on the post that helped you.
Page 1 of 1 (8 items)