想解开下拉列表吗?

时间:2013-07-26 09:27:37

标签: c# asp.net sql database

我已经创建了web应用程序,我想使用两个dropdownlist。当我从dropdown1中选择一些值影响dropdown2但是第一次没有得到清楚的值绑定可以任何人请帮助我

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Select sel_obj = new Select();
        #region Load Department Name
        if (departchk.Checked==true && DropDownList1.SelectedIndex==1)
        {

            DataTable DT = sel_obj.select_Dept_Name();
            recordddl.DataSource = DT;
            recordddl.DataTextField = "Department_Name";
            //departmentDrop.DataValueField = "Department_ID";
            recordddl.DataBind();
            recordddl.Items.Insert(0, new ListItem("--Select--", "0"));
        }
        #endregion
        #region Load Employee Name
        else if(departchk.Checked==true && DropDownList1.SelectedIndex==2)
        {

            DataTable DS = sel_obj.select_Emp_Name();
            recordddl.DataSource = DS;
            recordddl.DataTextField = "Employee_Name";
            recordddl.DataBind();
            recordddl.Items.Insert(0, new ListItem("--Select--", "0"));
        }
        #endregion
        #region Load Location Name
        else if(departchk.Checked==true && DropDownList1.SelectedIndex==3)
        {
            DataTable DD2 = sel_obj.select_Location_Name();
            recordddl.DataSource = DD2;
            recordddl.DataTextField = "Location_Name";
            recordddl.DataBind();
            recordddl.Items.Insert(0, new ListItem("--Select--", "0"));
        }
        #endregion
    }

7 个答案:

答案 0 :(得分:3)

就像DropDownList1.Items.Clear();

一样简单

Check a complete example here

答案 1 :(得分:1)

您可以通过将DataSource设置为null,然后调用DataBind来“取消绑定”下拉列表。

答案 2 :(得分:1)

我在SharePoint 2013中遇到了同样的问题(使用.NET 4.5),发现我必须设置所有这些

ddl.Items.Clear();
ddl.SelectedIndex = -1;
ddl.SelectedValue = null;

在我能够这样做之前重新绑定我的ddl

ddl.DataSource = list;
ddl.DataTextField = displayField;
ddl.DataValueField = valueField;
ddl.DataBind();

没有收到错误' ddl有一个无效的SelectedValue,因为它在项目列表中不存在...'

答案 3 :(得分:0)

在绑定之前写下这个。

DropDownList1.Items.Clear();

答案 4 :(得分:0)

有两件事

下拉列表应为runat='server'

  1. 您想重置selectedindex =0.
  2. 从下拉列表中清除收藏项目 然后使用dropdownlist.items.Clear();

答案 5 :(得分:0)

<asp:DropDownList ID="ddlcustomer" runat ="server" OnSelectedIndexChanged="ddlcustomer_SelectedIndexChanged" AutoPostBack ="true" ></asp:DropDownList>

试试此代码

答案 6 :(得分:0)

当您取消绑定DropdownList时,只需指定

即可
if (ddCountryName.SelectedItem != null)
    LoadDivisionStateName();
else
    ddDivisionStateName.Items.Clear();