title: Getting Checked value for each item in a CheckBoxList
body: <p>I have a list of options on my site that a user can select. What I want to do is provide functionality to limit the amount of content the user gets based on his selection in a CheckBoxList.
  Once he's selected what he wants, he'll click Save and his selection will be written to the database.
  The CheckBoxList is initially populated from the Modules table. This provides a list of modules that the user can select.
  When he clicks Save, the code needs to loop through this CheckBoxList and "pick out" the values for the CheckBoxes that were checked, disregarding the ones that weren't.</p>
  
  <p>The problem is that whether a CheckBox is checked or not, the debugger returns a <code>False</code> value for the <code>CheckBoxList.Items(i).Selected</code> property.</p>
  
  <p>Here's my code:</p>
  
  <p><strong>NOTE:</strong> I am aware that the SQL code here is open to injection attacks. I've been told that I shouldn't concern myself with it, although, ordinarily, I'd do it differently.</p>
  
  <pre><code>Private Sub AddUpdateOrg(ByVal OrganizationName As String, ByVal Action As String, Optional ByVal Target As Integer = Nothing)
      ' ###############################################################
      ' #                ADD OR UPDATE AN ORGANIZATION                #
      ' # ============================================================#
      ' # PARAMETERS                                                  #
      ' # ----------                                                  #
      ' # OrganizationName As String                                  #
      ' # The organization name that is to be stored in the database. #
      ' #                                                             #
      ' # Action As String                                            #
      ' # What action will be taken by this procedure, add or update. #
      ' #                                                             #
      ' # Optional Target As Integer (Default: Nothing)               #
      ' # If updating an existing record, specify the OrganizationID  #
      ' # of the target organization.                                 #
      ' ###############################################################
  
      ' Get the list of Modules selected for this organization
      Dim ModuleList As New List(Of Integer)
      For i As Integer = 0 To chkModules.Items.Count - 1
          If chkModules.Items(i).Selected Then
              ModuleList.Add(chkModules.Items(i).Value)
          End If
      Next
  
      Dim sql As String = Nothing
  
      Select Case Action
          Case "Add"
              sql = "insert into Organizations(OrganizationName) values ('" &amp; OrganizationName &amp; "')"
  
          Case "Update"
              sql = "update Organizations set OrganizationName = '" &amp; OrganizationName &amp; "' " &amp; _
                  "where OrganizationID = " &amp; Target
      End Select
  
      Dim dr As SqlDataReader = d.GetReader("select * from OrgModules where OrgID = " &amp; Target)
      If dr.HasRows Then
          dr.Close()
          UpdateModules(Target, ModuleList, "update")
      Else
          dr.Close()
          UpdateModules(Target, ModuleList, "insert")
      End If
      d.DoCommand(sql)
  End Sub
  </code></pre>
  
  <p>Its possible that this behavior is the result of a postback from the save button. If this is the case, I'm not sure how to fix it so any help will be greatly appreciated.</p>
  
  <p><strong>EDIT 1</strong> Upon further examination of the code, I've reconsidered the possibility that this issue is caused by a postback as the CheckBoxList is not bound on page load.</p>