当我尝试从下拉框“不能在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();
}
答案 0 :(得分:12)
更改此行:
DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString())
.Selected = true;
到此:
DropDownList1.SelectedValue = ViewState["MyFilter"].ToString();
问题是你已经有一个选定的项目(可能是列表中的第一个),而你正在搜索另一个项目,也选择了它。请注意,拥有多个所选项目对ListBox
和CheckListBox
有效,但对DropDownList
无效。
选择其中一个ListItem
不会自动取消选择ListItemColletion
中的其他项目。
答案 1 :(得分:5)
真的非常简单,因为Adrian提到当您在下拉列表中选择了一个项目,然后在代码的其他地方中选择了另一个项目时会发生此错误。
要解决此问题,请在GetGroupNameList
上设置一个制动点,如果在此行引发错误:
DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString()).Selected = true;
将以下代码行放在它上面:
DropDownList1.ClearSelection();
如果该行没有抛出错误,则意味着在GetGroupNameList
方法调用之后完成第二次选择,在这种情况下,在调用DropDownList1.ClearSelection();
之后直接放置GetGroupNameList
答案 2 :(得分:2)
DropDownList1.ClearSelection();
DropDownList1.FindByValue("parameter").Selected = true;
确保您没有将多个ddls数据绑定到同一数据源。被选中是一个项目的属性,因此,如果不同的ddls从同一个数据源中选择不同的项目,每个ddls最终会选择多个项目,这可能就是这里发生的事情..
答案 3 :(得分:2)
你可以尝试:
DropDownList1.ClearSelection();
行前:
DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString())
.Selected = true;
答案 4 :(得分: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;
}
}