【问题标题】:Error: foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.ListItem'错误:foreach 语句无法对“System.Web.UI.WebControls.ListItem”类型的变量进行操作
【发布时间】:2014-02-07 07:10:16
【问题描述】:

我正在尝试使用此代码将多个选定项从一个 ListBox 移动到另一个 ListBox

protected void imgbtnMoveRightListBox_Click(object sender, ImageClickEventArgs e)
{
    foreach (var item in lstboxSkill.SelectedItem)
    {
        lstBBoxSkill2.Items.Add(item);
    }
}

但它显示一个错误

foreach 语句不能对类型变量进行操作 'System.Web.UI.WebControls.ListItem' 因为 'System.Web.UI.WebControls.ListItem' 不包含公共 'GetEnumerator' 的定义

我不知道为什么会出现这个错误。

请帮我解决它

【问题讨论】:

  • lstboxSkill.SelectedItem 不是“列表”,只是列表中被点击的一项触发了事件。因此,foreach 没有意义。
  • 您的代码听起来像是项目中的每个项目,这在逻辑上是错误的..!!

标签: c# asp.net


【解决方案1】:

检查此代码。

protected void imgbtnMoveRightListBox_Click(object senderImageClickEventArge)

    {
    foreach (ListItem item in lstboxSkill.Items)
    {
        lstBBoxSkill2.Items.Add(item);
    }
}

【讨论】:

    【解决方案2】:

    请检查我创建的这个快照,它工作正常。 代码后面的代码如下:

    protected void Page_Load(object sender, EventArgs e)
        {
            lstboxSkill.Items.Add("ASP.Net");
            lstboxSkill.Items.Add("C#");
            lstboxSkill.Items.Add("AJAX");
            lstboxSkill.Items.Add("JavaScript");
            lstboxSkill.Items.Add("HTML");
            lstboxSkill.Items.Add("HTML5");
            lstboxSkill.Items.Add("JQuery");
        }
    protected void imgbtnMoveRightListBox_Click(object sender, EventArgs e)
        {
            foreach (ListItem Item in lstboxSkill.Items)
            {
                if (Item.Selected == true)
                {
                    lstBBoxSkill2.Items.Add(Item);
                }
            }            
        }
    

    【讨论】:

    • 您的代码非常好,即使我在单独的应用程序中尝试过它并且在那里运行良好,但不知道为什么在我的应用程序中它不起作用..在我的应用程序中我是动态的从下拉选择的值绑定第一个列表框,然后根据第一个列表框项我正在填充第二个列表框
    • 你能把你为绑定第一个列表框和第二个列表框写的代码发给我吗@akvickyit7
    【解决方案3】:

    这是因为 SelectedItem 属性仅返回所选项目列表中索引最低的项目。您应该将代码更改为

    protected void imgbtnMoveRightListBox_Click(object sender, EventArgs e)
        {
            foreach (ListItem Item in lstboxSkill.Items)
            {
                if (Item.Selected == true)
                {
                    lstBBoxSkill2.Items.Add(Item);
                }
            }            
        }
    

    并设置两个列表框 SelectionMode="Multiple"。

    希望这会对您有所帮助。不要忘记标记为答案

    谢谢 所罗门 S.

    【讨论】:

    • yes SOLOMON SARKAR 代码正在运行,但问题在于 lstboxSkill 始终选择第一项为真,而不是其他项。它没有得到除了第一个被选中的项目。
    • No No 我已经检查过它并且即使您选择任何索引而不是第一个索引,它也可以正常工作。 @akvickyit7
    【解决方案4】:

    lstboxSkill.SelectedItem 是一个ListItem,它既不是数组也不是实现System.Collections.IEnumerable<T>System.Collections.Generic.IEnumerable<T> 接口的对象集合,因此不可能对它执行foreach

    我想这就是你要找的东西:

    foreach (var item in lstboxSkill.Items)
    {
        if (item.Selected)
        {
            lstBBoxSkill2.Items.Add(item);
        }
    }
    

    【讨论】:

    • 您需要先验证该项目是否被选中,然后才能添加它,不是吗?
    • 这将添加列表中的所有项目。我认为目的是只添加选定的项目。
    • 我已经更改了答案,感谢您的输入。
    【解决方案5】:
    protected void imgbtnMoveRightListBox_Click(object sender, EventArgs e)
    {
        lbltxt.Visible = false;
        if (ListBox1.SelectedIndex >= 0) // in this we are checking whether a single item is clicked.
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)  // we are looping through the list box items
            {
                if (ListBox1.Items[i].Selected) // finding the selected items
                {
                    if (!arraylist1.Contains(ListBox1.Items[i]))
                    {
                        arraylist1.Add(ListBox1.Items[i]); //if found then adding those items to the array list
                    }
                }
            }
            for (int i = 0; i < arraylist1.Count; i++)
            {
                if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
                {
                    ListBox2.Items.Add(((ListItem)arraylist1[i])); // we are adding the array elements to the second list box 
                }
                ListBox1.Items.Remove(((ListItem)arraylist1[i]));
            }
            ListBox2.SelectedIndex = -1; 
        }
        else
        {
            lbltxt.Visible = true;
            lbltxt.Text = "Please select atleast one in Listbox1 to move";
        }
    }
    

    Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 2019-02-22
      • 2015-02-10
      相关资源
      最近更新 更多