title: Preselected checkbox using checkboxlist control in asp.net using VB
body: <p>How do I make one of the items in my checkboxlist automatically selected upon page load? The items in my checkboxlist come from a SQL DB and I want one of the fields to be automatically selected. I have found simple solutions to this question if I was not retrieving my checkboxlist items from a Database, but I am having trouble figuring out how to do this based on my situation. Any help would be greatly appreciated.</p>
  
  <p>Here is my code for my 2 relevant pages:</p>
  
  <p><strong>ALCounties.aspx.vb</strong></p>
  
  <pre><code>Imports System.Collections.Generic
  
  Partial Class ALCounties
  Inherits System.Web.UI.Page
  
  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim chkValues As Dictionary(Of String, Double) = _
          New Dictionary(Of String, Double)
  
  
      For Each Item As ListItem In CheckBoxList1.Items
          If (Item.Selected) Then
              If (Not Item.Value Is Nothing And Not String.IsNullOrEmpty(Item.Value())) Then
                  chkValues(Item.Text.ToString) = Convert.ToDouble(Item.Value)
              Else
                  chkValues(Item.Text.ToString) = 0
              End If
  
          End If
      Next Item
  
      Session("CheckedItems") = chkValues
      Response.Redirect("Cart.aspx")
  End Sub
  End Class
  </code></pre>
  
  <p><strong>ALCounties.aspx</strong></p>
  
  <pre><code>&lt;asp:CheckBoxList ID="CheckBoxList1" runat="server" 
                  DataSourceID="SqlDataSource1" DataTextField="AL_County" 
                  DataValueField="AL_Fee"&gt;
              &lt;/asp:CheckBoxList&gt;
         &lt;/div&gt;
              &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server"        ConnectionString="&lt;%$ ConnectionStrings:advancedleadsConnectionString %&gt;" 
                  SelectCommand="SELECT [AL_Fee], [AL_County]  FROM [AL]"&gt;   &lt;/asp:SqlDataSource&gt;
  
          &lt;p&gt;&amp;nbsp;&lt;/p&gt;
          &lt;p&gt;
          &lt;asp:Button ID="Button1" runat="server" Text="Submit" /&gt;
          &lt;/p&gt;
  </code></pre>