不能使用C#在DropDownList中选择多个项目 [英] Cannot have multiple items selected in a DropDownList using C#

查看:1093
本文介绍了不能使用C#在DropDownList中选择多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试选择从下拉框中的项目我得到这个错误无法在DropDownList中选择多个项目。是否有人可以帮助我,我不知道为什么我收到此。这里是我的code:

 私人无效Bind_GridView()
{
this.BindGroupNameList(DropDownList1);
} 私人无效GetGroupNameList(DropDownList的DropDownList1)
    {
        字符串strConnString = System.Configuration.ConfigurationManager.ConnectionStrings [MyConnectionString]的ConnectionString。
        SqlConnection的CON2 =新的SqlConnection(strConnString);
        SqlDataAdapter的SDA =新的SqlDataAdapter();
        CMD1的SqlCommand =新的SqlCommand(选择不同的名称+
                        从MyTable的);        cmd1.Connection = CON2;
        con2.Open();        DropDownList1.DataSource = cmd1.ExecuteReader();
        DropDownList1.DataTextField =姓名;
        DropDownList1.DataValueField =姓名;
        DropDownList1.DataBind();
        con2.Close();
        DropDownList1.Items.FindByValue(的ViewState [myfilter的。的ToString())
                .Selected = TRUE;
    }   //有关项目的变化
    保护无效NameChanged(对象发件人,EventArgs的发送)
    {
        DropDownList的DropDownList1 =(DropDownList的)寄件人;
        的ViewState [myfilter的] = DropDownList1.SelectedValue;
        this.Bind_GridView();
    }

和这里是我在dropdownbox ASPX

 < ASP:DropDownList的ID =DropDownList1=服务器的AutoPostBack =真OnSelectedIndexChanged =NameChanged
                        DataTextField =名称DataValueField =姓名
                        AppendDataBoundItems =真正的>
                        < ASP:ListItem的文本=ALLVALUE =ALL>< / ASP:ListItem的>
                        < ASP:ListItem的文本=十佳VALUE =10>< / ASP:ListItem的>
                    < / ASP:DropDownList的>

下面是code为页面加载速度:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {        如果(!Page.IsPostBack)
        {            的ViewState [myfilter的] =ALL;
            this.Bind_GridView();
        }}

下面是调用GetGroupNameList方法:

 私人无效Bind_GridView()
    {
        DataTable的DT =新的DataTable();
        字符串strConnString = System.Configuration.ConfigurationManager.ConnectionStrings [MyConnectionString]的ConnectionString。
        SqlConnection的CON =新的SqlConnection(strConnString);
        SqlDataAdapter的SDA =新的SqlDataAdapter();
        CMD的SqlCommand =新的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()。

问题是,你已经有了一个选择的项目(可能在​​列表中的第一个),你正在寻找另一种已将其选定。请记住,有多项选择的有效期为的ListBox CheckListBox ,而不是一个 DropDownList的

使所选列表项的一个不会自动取消选择在其他项目 ListItemColletion

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

    }

解决方案

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.

这篇关于不能使用C#在DropDownList中选择多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆