title: Display items in datalist upon checking checkboxlist
body: <p>I had data bind checkboxlist to a SQL Server, which display the items in the "Name" (using movie as an example: comedy, action, horror) column of my database . The checkboxlist acts as a filter so that when user checked on the checkboxes, the related movie would appear. </p>
  
  <p>I had managed to databind the checkboxlist. The value of the checkboxes have value that are bind to the "CategoryId" of the database. But I have no idea on how to proceed further, which is to display datalist of movie poster(images) when the checkbox is checked.  </p>
  
  <p>For example, when I check the "Comedy" checkbox, the movies poster(datalist) that belong to that genre would appear. </p>
  
  <p>Here is the code that I have done so far, default.aspx: </p>
  
  <pre><code>       &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
              ConnectionString="&lt;%$ ConnectionStrings:DVDShopConnectionString %&gt;" 
              SelectCommand="SELECT [ProductID], [Title], [Image1FileName] FROM [Product]"&gt;&lt;/asp:SqlDataSource&gt;
  
  
          &lt;asp:SqlDataSource ID="SqlDataSource2" runat="server" 
              ConnectionString="&lt;%$ ConnectionStrings:DVDShopConnectionString %&gt;" 
              SelectCommand="SELECT * FROM [Category]"&gt;&lt;/asp:SqlDataSource&gt;
          &lt;br /&gt;
  
  
          &lt;asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" 
              DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="CategoryID" 
              onselectedindexchanged="CheckBoxList1_SelectedIndexChanged"&gt;
          &lt;/asp:CheckBoxList&gt;
  
  
  
  &lt;asp:datalist runat="server" DataKeyField="ProductID" DataSourceID="SqlDataSource1" 
              RepeatColumns="4" ID="DataList1" &gt;
      &lt;ItemTemplate&gt;
  
  
          &lt;asp:Image ID="Image1" runat="server" 
           ImageUrl='&lt;%# Eval("Image1FileName", "~/ProductImages/{0}") %&gt;'  /&gt;
           &lt;br /&gt; 
  
          &lt;asp:Label ID="TitleLabel" runat="server" Text='&lt;%# Eval("Title") %&gt;' /&gt;
          &lt;br /&gt;
  
  
          &lt;br /&gt;
  &lt;br /&gt;
      &lt;/ItemTemplate&gt;
          &lt;/asp:datalist&gt; 
  </code></pre>
  
  <p>Behind code: </p>
  
  <pre><code>private SqlDataReader getReader()
  {
      //get connection string from web.config
      string strConnectionString = ConfigurationManager.ConnectionStrings["DVDShopConnectionString"].ConnectionString;
      SqlConnection myConnect = new SqlConnection(strConnectionString);
  
      string strCommandText = "SELECT CategoryID, Name  from Category";
  
      SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
      myConnect.Open();
  
       //DataList1.DataSource = reader; 
       DataList1.DataBind();
      // CommandBehavior.CloseConnection will automatically close connection
      SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
      return reader;
  }
  
  
  
  
  protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
  {
  
  
      for (int i = 0; i &lt; CheckBoxList1.Items.Count; i++)
      {
          if (CheckBoxList1.Items[i].Selected == true)
          {
              //items should be filter here.. 
  
          }
      }
  } 
  </code></pre>
  
  <p>Any suggestions or ideas are greatly appreciated. </p>