简体   繁体   English

选中复选框后显示数据列表中的项目

[英]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 . 我有一个数据绑定复选框列表到SQL Server,该列表在数据库的“名称”(以电影为例:喜剧,动作,恐怖)列中显示项目。 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. 复选框的值具有绑定到数据库的“ CategoryId”的值。 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: 这是到目前为止我完成的代码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. 任何建议或想法,我们将不胜感激。

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. 摆脱该getReader()方法,只要使用SqlDataSource,就不需要所有数据。 Simply do this at Page_Load 只需在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 ) CheckBoxList1_SelectedIndexChanged ,获取所有检查的值并将其连接到电影查询中,例如SELECT [ProductID], [Title], [Image1FileName] FROM [Product] WHERE CategoryId IN ( 将ID放在此处 )

  3. Set this query as the command for SqlDataSource1 将此查询设置为SqlDataSource1的命令

  4. Call DataList1.DataBind(); 调用DataList1.DataBind();

Please, test it and give me any feedback. 请进行测试,并给我任何反馈。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM