使用C#不能在DropDownList中选择多个项目

当我尝试从下拉框“不能在DropDownList中选择多个项目”中选择项目时,我收到此错误。 有人可以帮助我,我不知道为什么我得到这个。 这是我的代码:

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(); } 

这是我在aspx中的下拉框

     

以下是页面加载的代码:

 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ViewState["MyFilter"] = "ALL"; this.Bind_GridView(); } } 

这是调用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(); } 

改变这一行:

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

对此:

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

问题是你已经有一个选定的项目(可能是列表中的第一个),而你正在搜索另一个项目也是如此。 请记住,拥有多个选定项对ListBoxCheckListBox有效,但对DropDownList无效。

选择其中一个ListItem不会自动取消选择ListItem中的其他项。

这真的非常简单,因为Adrian提到当你在下拉列表中选择一个项目然后在你的代码中的其他地方选择另一个项目时会发生这个错误。

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

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

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

 DropDownList1.ClearSelection(); 

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

 DropDownList1.ClearSelection(); DropDownList1.FindByValue("parameter").Selected = true; 

确保您没有将多个ddls数据绑定到同一数据源。 被选中是一个项目的属性,因此,如果不同的ddls从同一个数据源中选择不同的项目,每个ddls最终会选择多个项目,这可能是这里发生的事情。

你可以尝试:

 DropDownList1.ClearSelection(); 

行前:

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

我建议你不要从aspx中添加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; } }