日期:2014-05-18  浏览次数:20500 次

DropDownList和ListView结合显示数据的问题
HTML code

<tr>
                <td>
                    请选择主机厂:<asp:DropDownList ID="ddlCustomer" runat="server" OnSelectedIndexChanged="ddlCustomer_changed">
                    </asp:DropDownList>    
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ListView ID="ListView1" runat="server" DataKeyNames="productid">
            <AlternatingItemTemplate>
            ........................................


C# code

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.Orders orders=new BLL.Orders();
                ddlCustomer.DataSource = orders.getCustomersDataSet();
                ddlCustomer.DataValueField = "customerid";
                ddlCustomer.DataTextField = "name";
                ddlCustomer.DataBind();
                ListView1.DataSource = orders.getProductsDataSetByCustomerId(int.Parse(ddlCustomer.SelectedValue));
                ListView1.DataBind();
            }
        }
        protected void ddlCustomer_changed(object sender, EventArgs e)
        {
            BLL.Orders orders = new BLL.Orders();
            ListView1.DataSource = orders.getProductsDataSetByCustomerId(int.Parse(ddlCustomer.SelectedValue));
            ListView1.DataBind();
        }


但测试是这样的 我在dropdownlist控件中选择了别的选项 而列表ListView并没有被更新,还是显示页面一打开的数据,难道是DropDownList控件的OnSelectedIndexChanged事件没有触发吗?但我的确更换其中的选项了啊

------解决方案--------------------
<asp:DropDownList AutoPostBack="true"

才会执行 OnSelectedIndexChanged