By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,083 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,083 IT Pros & Developers. It's quick & easy.

VB.NET Selecting Items In CheckBoxList

P: 21
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)

Expand|Select|Wrap|Line Numbers
  1. Dim UNameStr As New Label
  2.  
  3.         'Get username
  4.         UNameStr.Text = gvUser.SelectedRow.Cells(3).Text
  5.  
  6.         'Get User's Roles
  7.         Dim userroles() As String = Roles.GetRolesForUser(UNameStr.Text)
  8.         Dim NewRoles As CheckBoxList
  9.  
  10.         'Find CheckBoxList
  11.         NewRoles = DirectCast(fvUserEdit.FindControl("cbRoles"), CheckBoxList)
  12.  
  13.         'Uncheck everything in the CheckBoxList
  14.         NewRoles.ClearSelection()
  15.  
  16.         'Select Roles User Is In
  17.         For Each li As ListItem In NewRoles.Items
  18.             For indexA As Integer = 0 To userroles.Length - 1
  19.                 If li.Value.ToString = userroles(indexA).ToString Then
  20.                     'If the values match, set the checkbox to true
  21.                     li.Selected = True
  22.                 End If
  23.             Next
  24.         Next
Mar 10 '14 #1
Share this Question
Share on Google+
1 Reply


pod
100+
P: 298
pod
try rebinding the datasource if you are using one
Mar 11 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.