绑定ASP ListView和Dropdownlist

时间:2014-02-10 08:22:36

标签: asp.net c#-4.0

我有一个项目类型的下拉列表和我所有项目的列表视图和我将它们绑定到每个项目的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]">

1 个答案:

答案 0 :(得分:0)

我假设你有一个下拉列表名称ddl和listview作为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
    }
    }