不能使用 C# 在 DropDownList 中选择多个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16068593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot have multiple items selected in a DropDownList using C#
提问by moe
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:
当我尝试从下拉框中选择一个项目“不能在 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();
}
and here is my dropdownbox in aspx
这是我在 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:
这是调用 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();
}
回答by Adriano Carneiro
Change this line:
更改此行:
DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString())
.Selected = true;
to this:
对此:
DropDownList1.SelectedValue = ViewState["MyFilter"].ToString();
The problem is that you already have a selected item (probably the first in the list) and you are searching for another an have it selected as well. Keep in mind that having multiple selected items is valid for ListBoxand CheckListBox, but not for a DropDownList.
问题是您已经有一个选定的项目(可能是列表中的第一个),并且您正在搜索另一个项目并选择了它。请记住,选择多个项目对ListBoxand有效CheckListBox,但对 a无效DropDownList。
Making one of the ListItemselected does not automatically unselect the other items in the ListItemColletion.
ListItem选择其中一项不会自动取消选择ListItemColletion.
回答by Denys Wessels
It's quite simple really, as Adrian mentioned this error occurs when you have an item in the drop down selected and then elsewhere in your codeyou select another item.
这真的很简单,正如 Adrian 提到的,当您在下拉列表中选择了一个项目,然后在代码的其他地方选择另一个项目时,就会发生此错误。
To fix the issue put a brake point on GetGroupNameListand if the error is thrown at this line:
要解决此问题,请打开制动点GetGroupNameList,如果在此行引发错误:
DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString()).Selected = true;
Place the following line of code above just above it:
将以下代码行放在其正上方:
DropDownList1.ClearSelection();
If that line doesn't throw an error it means the second selection is done after the GetGroupNameListmethod call, in that case place DropDownList1.ClearSelection();straight after the call for GetGroupNameList
如果该行没有抛出错误,则意味着在GetGroupNameList方法调用之后进行了第二次选择,在这种情况下,DropDownList1.ClearSelection();直接在调用之后放置GetGroupNameList
回答by Pranali Desai
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..
确保您没有将多个 ddls 数据绑定到同一个数据源。被选中是项目的一个属性,因此,如果不同的 ddls 从同一数据源中选择不同的项目,则每个 ddls 最终都会选择多个项目,这可能就是这里发生的事情。
回答by Full Time Skeleton
You could try:
你可以试试:
DropDownList1.ClearSelection();
Before the line:
上线前:
DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString())
.Selected = true;
回答by Damith
I suggest you to not adding default values of DropDownList from aspx and clear all the items before bind data.
我建议您不要从 aspx 添加 DropDownList 的默认值,并在绑定数据之前清除所有项目。
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="NameChanged"
DataTextField="Name" DataValueField="Name" >
</asp:DropDownList>
and change GetGroupNameList method as below
并更改 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;
}
}

