title: asp.net vb CheckBoxList Selected from CSV
body: <p>I am learning asp.net and needed to have a CheckBoxList which items will be initially selected if the are in a CSV string from a database.</p>
  
  <p>I have got it working although I just wondered if I have gone about it the best way as it seemed a little long winded?</p>
  
  <p>Thanks for any help provided.</p>
  
  <p><strong>ASPX</strong></p>
  
  <pre><code>&lt;asp:CheckBoxList ID="rh_type" runat="server" CssClass="chkbox" 
  RepeatLayout="Flow" CausesValidation="True"&gt;
  &lt;asp:ListItem&gt;House&lt;/asp:ListItem&gt;
  &lt;asp:ListItem&gt;Flat/Apartment&lt;/asp:ListItem&gt;
  &lt;asp:ListItem&gt;Bungalow&lt;/asp:ListItem&gt;
  &lt;asp:ListItem&gt;Any&lt;/asp:ListItem&gt;
  &lt;/asp:CheckBoxList&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  &lt;asp:Button ID="Button1" runat="server" Text="Button" /&gt;
  </code></pre>
  
  <p><strong>CODE</strong></p>
  
  <pre><code>    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
      Dim i As Integer
      Dim str_rh_type As String = "House,Bungalow"
      Dim split As String() = str_rh_type.Split(","c)
  
      For Each s As String In split
          'Response.Write(s &amp; "&lt;br /&gt;")
  
          For i = 0 To rh_type.Items.Count - 1
              If rh_type.Items(i).Text = s Then
  
                  rh_type.Items(i).Selected = True
  
              End If
          Next
  
      Next s
  
  End Sub
  </code></pre>
  
  <p>Thanks again
  J.</p>