user1501969
user1501969

Reputation: 127

Display items in datalist upon checking checkboxlist

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.

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.

For example, when I check the "Comedy" checkbox, the movies poster(datalist) that belong to that genre would appear.

Here is the code that I have done so far, default.aspx:

       <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DVDShopConnectionString %>" 
            SelectCommand="SELECT [ProductID], [Title], [Image1FileName] FROM [Product]"></asp:SqlDataSource>


        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DVDShopConnectionString %>" 
            SelectCommand="SELECT * FROM [Category]"></asp:SqlDataSource>
        <br />


        <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="CategoryID" 
            onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
        </asp:CheckBoxList>



<asp:datalist runat="server" DataKeyField="ProductID" DataSourceID="SqlDataSource1" 
            RepeatColumns="4" ID="DataList1" >
    <ItemTemplate>


        <asp:Image ID="Image1" runat="server" 
         ImageUrl='<%# Eval("Image1FileName", "~/ProductImages/{0}") %>'  />
         <br /> 

        <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
        <br />


        <br />
<br />
    </ItemTemplate>
        </asp:datalist> 

Behind 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 < CheckBoxList1.Items.Count; i++)
    {
        if (CheckBoxList1.Items[i].Selected == true)
        {
            //items should be filter here.. 

        }
    }
} 

Any suggestions or ideas are greatly appreciated.

Upvotes: 0

Views: 2640

Answers (1)

Andre Calil
Andre Calil

Reputation: 7692

I'll suggest an answer that I hadn't tested, so please give me any feedback. Let's see:

  1. Get rid of that getReader() method, you don't need all data as long as you are using a SqlDataSource. Simply do this at Page_Load

    if(!this.IsPostBack) { this.CheckBoxList1.DataBind(); }

  2. At CheckBoxList1_SelectedIndexChanged, get all checked values and concatenate them on a query for movies, like SELECT [ProductID], [Title], [Image1FileName] FROM [Product] WHERE CategoryId IN ( put the IDs here )

  3. Set this query as the command for SqlDataSource1

  4. Call DataList1.DataBind();

Please, test it and give me any feedback.

Regards

Upvotes: 1

Related Questions