我将数据绑定复选框列表到 SQL Server,它在我的数据库的“名称”(以电影为例:喜剧、动作、恐怖)列中显示项目。复选框列表充当过滤器,因此当用户选中复选框时,相关电影就会出现。
我设法对复选框列表进行了数据绑定。复选框的值具有绑定到数据库的“CategoryId”的值。但我不知道如何进一步进行,即在选中复选框时显示电影海报(图像)的数据列表。
例如,当我选中“喜剧”复选框时,会出现属于该类型的电影海报(数据列表)。
这是我到目前为止所做的代码,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>
后面的代码:
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..
}
}
}
非常感谢任何建议或想法。