问题
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();
}
回答1:
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 ListBox
and CheckListBox
, but not for a DropDownList
.
Making one of the ListItem
selected does not automatically unselect the other items in the ListItemColletion
.
回答2:
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 code you select another item.
To fix the issue put a brake point on GetGroupNameList
and if the error is thrown at this line:
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 GetGroupNameList
method call, in that case place DropDownList1.ClearSelection();
straight after the call for GetGroupNameList
回答3:
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..
回答4:
You could try:
DropDownList1.ClearSelection();
Before the line:
DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString())
.Selected = true;
回答5:
I suggest you to not adding default values of DropDownList from aspx and clear all the items before bind data.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="NameChanged"
DataTextField="Name" DataValueField="Name" >
</asp:DropDownList>
and change GetGroupNameList method as below
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;
}
}
来源:https://stackoverflow.com/questions/16068593/cannot-have-multiple-items-selected-in-a-dropdownlist-using-c-sharp