【问题标题】:Cannot have multiple items selected in a DropDownList using C# [closed]不能使用 C# 在 DropDownList 中选择多个项目 [关闭]
【发布时间】:2013-04-10 17:17:12
【问题描述】:

当我尝试从“不能在 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 中的下拉框

<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>

这是页面加载的代码:

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

    }

【问题讨论】:

  • page_load 中是否有任何代码,如果是,那么请。也发一下。
  • Ratina,我添加了页面加载代码。谢谢
  • 好吧,在 GetGroupNameList(DropDownList DropDownList1) 之前的 DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString()) .Selected = true;插入这个--> DropDownList1.selectedIndex=-1;
  • 已添加但仍然是同样的问题。
  • 谁告诉你可以在 DropDownList 中选择多个项目? DropDownList是一个组合框,在里面只能选择一项,多选就得用ListBox之类的控件了。我什至惊讶地看到每个人都在这里提出答案。

标签: c# asp.net dropdownbox


【解决方案1】:

改变这一行:

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

到这里:

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

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

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

【讨论】:

  • 谢谢阿德里安,我已经做出了改变,但我仍然遇到同样的错误
  • @moe 确保您没有两个具有相同值的项目
  • 我没有重复项,而且我从我的表中获得了不同的名称,正如您在我的选择语句中看到的那样
  • 谢谢。已经烦了我几个小时了!!
  • 我本打算为赏金回答这个问题,但这是正确的答案。我不明白为什么它不适合你。问题是数据绑定后,您无法在列表框中的项目属性上设置值。可以这样想……绑定后,列表框OWNS那些项目,你需要通过列表框来改变选中的项目。我可以解释幕后发生的事情,但由于这里已经有了正确的答案,我不确定这样做的价值。基本上,您正在抛出您没有考虑过的事件。
【解决方案2】:

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

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

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

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

DropDownList1.ClearSelection();

如果该行没有引发错误,则表示第二次选择是在 GetGroupNameList 方法调用之后完成的,在这种情况下,将 DropDownList1.ClearSelection(); 直接放在对 GetGroupNameList 的调用之后

【讨论】:

  • Deni,谢谢伙计,但我尝试了你的所有建议,但我仍然得到了副本。我还在上一篇文章中添加了页面加载代码,看看你是否能看到我做错了什么。谢谢
  • 你在哪里调用 GetGroupNameList?
  • 我在 Bind_GridView 方法中调用它。我刚刚更新了我的初始帖子并添加了 Bind_GridView 方法。请。往上看。谢谢
  • 我仍然有这个问题,有人可以帮忙吗?
  • 压缩你的项目并将其发布到谷歌驱动器上并与我分享链接,我会看看
【解决方案3】:
DropDownList1.ClearSelection();
DropDownList1.FindByValue("parameter").Selected = true; 

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

【讨论】:

    【解决方案4】:

    你可以试试:

    DropDownList1.ClearSelection();
    

    行前:

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

    【讨论】:

      【解决方案5】:

      我建议您不要从 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;
              }
          }
      

      【讨论】:

      • 我认为这正是问题的关键所在。每次用户进行选择时,您都会将下拉列表重新绑定到数据源,但是您有 AppendDataBoundItems="true" 可以防止以前的值在重新绑定时被清除。您需要在每次重新绑定之前设置 AppendDataBoundItems="false" (默认值)或清除 Items 集合。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多