我有一个项目类型的下拉列表和一个包含我所有项目和 2 个数据源的列表视图,我将它们绑定到每个数据源,当我从下拉列表中选择每种类型时,它会从列表视图中过滤项目,但我不知道如何显示所有列表项? 它仅按类型过滤数据。如何在从数据源读取数据时将"全部显示"项添加到下拉列表中。
列表视图数据源:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [BookTbl] WHERE ([TypeId] = @TypeId)">
<SelectParameters>
<asp:ControlParameter ControlID="BookListddl" Name="TypeId" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
下拉列表数据源:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [BookTypeTbl]">
我想你有一个下拉列表名称 ddl 和列表视图作为 lvMyItems。
Protected void Page_Load(Object sender,EventArgs e)
{
if (this.IsPostBack)
{
string command = SqlDataSource2.SelectCommand; // added just for debug purpose
SqlDataSource1.SelectCommand = "SELECT * FROM [BookTypeTbl]";
SqlDataSource1.DataBind();
ddl.DataBind();
ddl.Items.Insert(0,"Show All");
}
}
Protected void ddl_SelectedIndexchanged(Object sender,EventArgs e)
{
if(ddl.SelectedValue > 0)
{
string command = SqlDataSource1.SelectCommand;
SqlDataSource1.SelectCommand = "SELECT * FROM [BookTbl] WHERE ([TypeId] = '"+ddl.SelectedValue+"'";
SqlDataSource1.DataBind();
}
else
{
string command = SqlDataSource1.SelectCommand;
SqlDataSource1.SelectCommand = "SELECT * FROM [BookTbl] ";
SqlDataSource1.DataBind();
//rebind your listview here
}
}