VB.NET Selecting Items In CheckBoxList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • billybob206
    New Member
    • Jun 2009
    • 21

    VB.NET Selecting Items In CheckBoxList

    Hello:

    I have a form that is used to update user's profile. The user can have multiple roles that I'm trying to display using a CheckBoxList.

    When initially selecting a user the code below works. If I select another user, the checkboxes in my form are not filled out (all are unselected). Additionally, if I update a user and then select another user, the checkboxes are filled correctly.

    Below is the code I am using. Thanks in advance for your help. (GV= Gridview FV = Formview)

    Code:
    Dim UNameStr As New Label
    
            'Get username
            UNameStr.Text = gvUser.SelectedRow.Cells(3).Text
    
            'Get User's Roles
            Dim userroles() As String = Roles.GetRolesForUser(UNameStr.Text)
            Dim NewRoles As CheckBoxList
    
            'Find CheckBoxList
            NewRoles = DirectCast(fvUserEdit.FindControl("cbRoles"), CheckBoxList)
    
            'Uncheck everything in the CheckBoxList
            NewRoles.ClearSelection()
    
            'Select Roles User Is In
            For Each li As ListItem In NewRoles.Items
                For indexA As Integer = 0 To userroles.Length - 1
                    If li.Value.ToString = userroles(indexA).ToString Then
                        'If the values match, set the checkbox to true
                        li.Selected = True
                    End If
                Next
            Next
    Last edited by NeoPa; Mar 10 '14, 09:06 PM. Reason: Tidied up the code to make it readable
  • pod
    Contributor
    • Sep 2007
    • 298

    #2
    try rebinding the datasource if you are using one

    Comment

    Working...