3

i am getting this error when i try to select an item from the drop-down box "Cannot have multiple items selected in a DropDownList". Can someone please help me i am not sure why i am getting this. here is my code:

private void Bind_GridView()
{
this.BindGroupNameList(DropDownList1);
}

 private void GetGroupNameList(DropDownList DropDownList1)
    {
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        SqlConnection con2 = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        SqlCommand cmd1 = new SqlCommand("select distinct Name" +
                        " from MyTable");

        cmd1.Connection = con2;
        con2.Open();

        DropDownList1.DataSource = cmd1.ExecuteReader();
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "Name";
        DropDownList1.DataBind();
        con2.Close();
        DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString())
                .Selected = true;
    }

   //on item change
    protected void NameChanged(object sender, EventArgs e)
    {
        DropDownList DropDownList1 = (DropDownList)sender;
        ViewState["MyFilter"] = DropDownList1.SelectedValue;
        this.Bind_GridView();
    }

and here is my dropdownbox in aspx

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="NameChanged"
                        DataTextField="Name" DataValueField="Name" 
                        AppendDataBoundItems="true">
                        <asp:ListItem Text="ALL" Value="ALL"></asp:ListItem>
                        <asp:ListItem Text="Top 10" Value="10"></asp:ListItem>
                    </asp:DropDownList>

Here is the code for the page load:

protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {

            ViewState["MyFilter"] = "ALL";
            this.Bind_GridView();


        }

}

here is the method that calls GetGroupNameList:

 private void Bind_GridView()
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand("sp_filter_Names");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@MyFilter", ViewState["MyFilter"].ToString());
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        GV_Test.DataSource = dt;
        GV_Test.DataBind();
        GetGroupNameList();

    }
4

5 回答 5

12

更改此行:

DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString())
              .Selected = true;

对此:

DropDownList1.SelectedValue = ViewState["MyFilter"].ToString();

问题是您已经有一个选定的项目(可能是列表中的第一个),并且您正在搜索另一个并选择它。请记住,选择多个项目对ListBoxand有效CheckListBox,但对 a无效DropDownList

选择其中一项ListItem不会自动取消选择ListItemColletion.

于 2013-04-17T19:32:33.383 回答
5

这真的很简单,正如 Adrian 提到的,当您在下拉列表中选择了一个项目,然后在代码的其他地方选择了另一个项目时,就会发生此错误。

要解决此问题,请设置刹车点GetGroupNameList,如果在此行引发错误:

DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString()).Selected = true;

将以下代码行放在其上方:

DropDownList1.ClearSelection();

如果该行没有抛出错误,则意味着第二次选择是在GetGroupNameList方法调用之后完成的,在这种情况下,DropDownList1.ClearSelection();直接在调用之后进行GetGroupNameList

于 2013-04-18T08:28:43.763 回答
2
DropDownList1.ClearSelection();
DropDownList1.FindByValue("parameter").Selected = true; 

Make sure you are not databinding multiple ddls to the same datasource. Being selected is an attribute of an item, therefore, if different ddls select different items from the same datasource, each of the ddls ends up with multiple items selected which is probably what is happening here..

于 2013-04-23T09:37:26.830 回答
1

你可以试试:

DropDownList1.ClearSelection();

行前:

DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString())
            .Selected = true;
于 2013-04-23T09:38:30.840 回答
1

我建议您不要从 aspx 添加 DropDownList 的默认值,并在绑定数据之前清除所有项目。

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="NameChanged"
                        DataTextField="Name" DataValueField="Name" >
                    </asp:DropDownList>

并更改 GetGroupNameList 方法如下

private void GetGroupNameList(DropDownList ddl)
    {
        ddl.Items.Clear();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        SqlConnection con2 = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        SqlCommand cmd1 = new SqlCommand("select distinct Name" +
                        " from MyTable");

        cmd1.Connection = con2;
        con2.Open();

        ddl.DataSource = cmd1.ExecuteReader();
        ddl.DataTextField = "Name";
        ddl.DataValueField = "Name";
        ddl.DataBind();
        con2.Close();

        ddl.Items.Insert(0, new ListItem("Top 10", "10"));
        ddl.Items.Insert(0, new ListItem("ALL", "ALL"));

        ListItem oListItem = ddl.Items.FindByValue(ViewState["MyFilter"].ToString());
        if(oListItem != null){
             ddl.ClearSelection();
             ddl.SelectedValue = oListItem.Value;
        }
    }
于 2013-04-24T16:59:14.867 回答